加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

Php Html Dom Parser没有得到和元素

发布时间:2020-12-13 17:30:52 所属栏目:PHP教程 来源:网络整理
导读:我正在使用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.non
我正在使用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;}
)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读