1
|
<?php
|
2
|
/**
|
3
|
* moves all css definitions from <body> into <head> section
|
4
|
* @param string $content
|
5
|
* @return string
|
6
|
*/
|
7
|
function doFilterCssToHead($content) {
|
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);
|
17
|
}
|
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);
|
24
|
}
|
25
|
return $content;
|
26
|
}
|