Php Html Dom Parser没有得到和元素

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了Php Html Dom Parser没有得到和元素脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用PHP Html Dom Parser来获取元素.但它并没有获得内在文本的元素.见下面的代码;

$htML = file_get_html($currentFile);
foreach($html->find('style') as $e){
  echo $e->plaintext;
}

我有这种类型的页面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>

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

谢谢

解决方法

您已经正确定位样式标记.但是您需要使用 – > innertext magic属性获取值.考虑这个例子:

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);

$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;}
)

脚本宝典总结

以上是脚本宝典为你收集整理的Php Html Dom Parser没有得到和元素全部内容,希望文章能够帮你解决Php Html Dom Parser没有得到和元素所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。