我通过简单的 html 解析器或 simple_html_dom.php 获取页面。我在页面中仅获取图像 h1 h2 h3 和 p 元素,并且我想从 anchor 标记中删除 href 属性。但出现错误。 根据官方文档,语法是..
删除一个属性,将其值设置为 null!
$e->href = null;
但是我收到错误。 这是我的代码。
<?php
include_once('simple_html_dom.php');
$html = file_get_html('http://www.lifehacker.com/');
$html=$html->find('h1 , h2 , h3 ,img, p');
$html->a["href"]=null;
foreach($html as $elements){
echo $elements.'<br>';
}
我想从每个元素中删除 href。意味着仅出现链接,但用户不应该能够打开它或定位它..
最佳答案
更新:当您仅检查 anchor 时,它会给您想要的结果吗?
foreach($html->find('a') as $element) {
if (isset($element->href)) {
$element->href = null;
}
}
echo $html;
关于javascript - 通过简单的html dom删除页面的所有href,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33327798/