使用htmldom解析器删除 href链接和标签

首先,我得到网页的 HTML,然后我删除通常出现在页面的左侧或右侧(不在页面正文中)的 href 链接。

首先,我得到网页的 HTML,然后我删除通常出现在页面的左侧或右侧(不在页面正文中)的 href 链接。

例子:

<a href='http://test.blogspot.com/2012/11/myblog.html'>London</a>

链接正在被删除,但不是它的标签,即“伦敦”。我如何删除 html 源代码中的完整行?我正在使用以下代码:

$string = strip_tags($html_source_code, '<a>', TRUE); 
function strip_tags($text, $tags = '', $invert = FALSE) {
      preg_match_all('/<(.+?)[\s]*\/?[\s]*>/si', trim($tags), $tags); 
      $tags = array_unique($tags[1]); 
      if(is_array($tags) AND count($tags) > 0) { 
        if($invert == FALSE) { 
          return preg_replace('@<(?!(?:'. implode('|', $tags) .')\b)(\w+)\b.*?>.*?</\1>@si', '', $text); 
        } 
        else { 
          return preg_replace('@<('. implode('|', $tags) .')\b.*?>.*?</\1>@si', '', $text); 
        } 
      } 
      elseif($invert == FALSE) { 
        return preg_replace('@<(\w+)\b.*?>.*?</\1>@si', '', $text); 
      } 
return $text; 
}
0

如果我使用你的代码,我得到一个致命的错误:不能重新声明 strip_tags()。

将名称函数更改为类似 my_strip_tags 的工作正常。

function my_strip_tags($text, $tags = '', $invert = FALSE) {
      preg_match_all('/<(.+?)[\s]*\/?[\s]*>/si', trim($tags), $tags); 
      $tags = array_unique($tags[1]); 
      if(is_array($tags) AND count($tags) > 0) { 
        if($invert == FALSE) { 
          return preg_replace('@<(?!(?:'. implode('|', $tags) .')\b)(\w+)\b.*?>.*?</\1>@si', '', $text); 
        } 
        else { 
          return preg_replace('@<('. implode('|', $tags) .')\b.*?>.*?</\1>@si', '', $text); 
        } 
      } 
      elseif($invert == FALSE) { 
        return preg_replace('@<(\w+)\b.*?>.*?</\1>@si', '', $text); 
      } 
return $text; 
}
$html_source_code = "Beginning of content ... <a href='http://test.blogspot.com/2012/11/myblog.html'>London</a> ... end of content.";
echo "<p>".$html_source_code."</p>";
$string = my_strip_tags($html_source_code, '<a>', TRUE);
echo "<p>".$string."</p>"; 

打印:

内容开头...London...内容结尾。

内容的开头……内容的结尾。

0
$link = "<a href='http://test.blogspot.com/2012/11/myblog.html'>London</a>";
function erraser($theLink, $checkTag){
    if(strpos($theLink, $checkTag) == true){
        for($i=0; $i< strlen($theLink); $i++){
        $link[$i] = '';
        return  $link[$i];
        }
       }else{
        return $theLink;
    }
}

现在,让我们看看这个:

所有你必须做的是给erraser()函数两个参数,然后链接的变量,以及任何文本来识别链接

如果您为 ex:echo erraser($link, 'href');执行此操作,它将删除链接,而return什么都没有。但是,如果您在echo erraser($link, '----');中给它----,则将给出链接london,这意味着它将检查它是否是链接并执行所需的功能

本站系公益性非盈利分享网址,本文来自用户投稿,不代表码文网立场,如若转载,请注明出处

(990)
找不到具有您请求的功能的树生成器:xml
上一篇
在 AppleScript中将字符串和变量连接成字符串
下一篇

相关推荐

发表评论

登录 后才能评论

评论列表(56条)