Project

General

Profile

1
<?php
2
/**
3
 * moves all css definitions from <body> into <head> section
4
 * @param string $content
5
 * @return string
6
 */
7
	function doFilterCssToHead($sContent) {
8
		// move css definitions into head section
9

    
10
		$sPattern1 = '/(?:<body.*?)(<link[^>]*?\"text\/css\".*?\/>)/si';
11
		$sPattern2 = '/(?:<body.*?)(<style[^>]*?\"text\/css\"[^>]*?>.*?<\/style>)/si';
12
		$aInsert = array();
13
		while(preg_match($sPattern1, $sContent, $aMatches)) {
14
			$aInsert[] = $aMatches[1];
15
			$sContent = str_replace($aMatches[1], '', $sContent); 
16
		}
17
		while(preg_match($sPattern2, $sContent, $aMatches)) {
18
			$aInsert[] = $aMatches[1];
19
			$sContent = str_replace($aMatches[1], '', $sContent);
20
		}
21
		$aInsert = array_unique($aInsert);
22
		if(sizeof($aInsert) > 0) {
23
			$sInsert = "\n".implode("\n", $aInsert)."\n</head>\n<body";
24
			$sContent = preg_replace('/<\/head>.*?<body/si', $sInsert, $sContent, 1);
25
		}
26
		return $sContent;
27
	}
(1-1/6)