4 |
4 |
* @param string $content
|
5 |
5 |
* @return string
|
6 |
6 |
*/
|
7 |
|
function doFilterCssToHead($content) {
|
|
7 |
function doFilterCssToHead($sContent) {
|
8 |
8 |
// move css definitions into head section
|
9 |
|
$pattern1 = '/(?:<body.*?)(<link[^>]*?\"text\/css\".*?\/>)/si';
|
10 |
|
$pattern2 = '/(?:<body.*?)(<style[^>]*?\"text\/css\"[^>]*?>.*?<\/style>)/si';
|
11 |
|
while(preg_match($pattern1, $content, $matches)==1) {
|
12 |
|
// loop through all linked CSS
|
13 |
|
$insert = $matches[1];
|
14 |
|
$content = str_replace($insert, '', $content);
|
15 |
|
$insert = "\n".$insert."\n</head>\n<body";
|
16 |
|
$content = preg_replace('/<\/head>.*?<body/si', $insert, $content);
|
|
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);
|
17 |
16 |
}
|
18 |
|
while(preg_match($pattern2, $content, $matches)==1) {
|
19 |
|
// loop through all inline CSS
|
20 |
|
$insert = $matches[1];
|
21 |
|
$content = str_replace($insert, '', $content);
|
22 |
|
$insert = "\n".$insert."\n</head>\n<body";
|
23 |
|
$content = preg_replace('/<\/head>.*?<body/si', $insert, $content);
|
|
17 |
while(preg_match($sPattern2, $sContent, $aMatches)) {
|
|
18 |
$aInsert[] = $aMatches[1];
|
|
19 |
$sContent = str_replace($aMatches[1], '', $sContent);
|
24 |
20 |
}
|
25 |
|
return $content;
|
|
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;
|
26 |
27 |
}
|
fixed filter to serve malformed/nested HTML also.