Project

General

Profile

1 1626 darkviper
<?php
2
/**
3
 * moves all css definitions from <body> into <head> section
4
 * @param string $content
5
 * @return string
6
 */
7 1656 darkviper
	function doFilterCssToHead($sContent) {
8 1626 darkviper
		// move css definitions into head section
9 1656 darkviper
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 1626 darkviper
		}
17 1656 darkviper
		while(preg_match($sPattern2, $sContent, $aMatches)) {
18
			$aInsert[] = $aMatches[1];
19
			$sContent = str_replace($aMatches[1], '', $sContent);
20 1626 darkviper
		}
21 1656 darkviper
		$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 1626 darkviper
	}