Php Html Dom Parser没有得到<style>和<script>元素

Sae*_*zal 4 html php simple-html-dom

我正在使用Php Html Dom Parser来获取元素.但它并没有获得内在文本的元素.见下面的代码;

$html = file_get_html($currentFile);
foreach($html->find('style') as $e){
  echo $e->plaintext;
}
Run Code Online (Sandbox Code Playgroud)

我有这种类型的页面CSS代码

<style type="text/css">
ul.gallery li.none { display:none;}
ul.gallery { margin:35px 24px 0 19px;}
</style>
<!--<![endif]-->
<style type="text/css">
body { background:#FFF url(images/bg.gif) repeat-x;}
</style>
Run Code Online (Sandbox Code Playgroud)

我想用内部文本获取每个元素.

谢谢

use*_*142 5

您已经正确定位style标记.但是您需要使用->innertextmagic属性来获取值.考虑这个例子:

include 'simple_html_dom.php';
$html_string = '<style type="text/css">ul.gallery li.none { display:none;}ul.gallery { margin:35px 24px 0 19px;}</style><!--<![endif]--><style type="text/css">body { background:#FFF url(images/bg.gif) repeat-x;}</style>';
$html = str_get_html($html_string); // or file_get_html in your case
$styles = array();
foreach($html->find('style') as $style) {
    $styles[] = $style->innertext;
}

echo '<pre>';
print_r($styles);
Run Code Online (Sandbox Code Playgroud)

$styles 应该输出:

Array
(
    [0] => ul.gallery li.none { display:none;}ul.gallery { margin:35px 24px 0 19px;}
    [1] => body { background:#FFF url(images/bg.gif) repeat-x;}
)
Run Code Online (Sandbox Code Playgroud)