【发布时间】:2019-03-05 16:13:00
【问题描述】:
这是一个不是我的脚本代码,我尝试修改它。它会搜索所有标签然后删除它们。您将如何修改代码以仅删除给定域或 url 的标签?例如,删除域标签:www.domainurl.com,删除所有标签为:
<a href="https://www.domainurl.com/refer/google-adsense/">fsdf</a>
<a title="Google Adsense" href="https://www.domainurl.com/refer/google-adsense/" target="_blank" rel="nofollow noopener">fgddf</a>
<a href="https://www.domainurl.com/page/pago">domain </a>
<a title="Google Adsense" href="https://www.googlead.com/refer/google-adsense/" target="_blank" rel="nofollow noopener">googled</a>
结果如下所示:
fsdf
fgddf
domain
<a title="Google Adsense" href="https://www.googlead.com/refer/google-adsense/" target="_blank" rel="nofollow noopener">google</a>
这是代码:
if (in_array ( 'OPT_STRIP', $camp_opt )) {
echo '<br>Striping links ';
//$abcont = strip_tags ( $abcont, '<p><img><b><strong><br><iframe><embed><table><del><i><div>' );
preg_match_all('{<a.*?>(.*?)</a>}' , $abcont , $allLinksMatchs);
$allLinksTexts = $allLinksMatchs[1];
$allLinksMatchs=$allLinksMatchs[0];
$j = 0;
foreach ($allLinksMatchs as $singleLink){
if(! stristr($singleLink, 'twitter.com'))
$abcont = str_replace($singleLink, $allLinksTexts[$j], $abcont);
$j++;
}
}
我试过这样做,但对我不起作用:
正则表达式:
使用 preg_match_all 在搜索中指定
preg_match_all('{<a.*?[^>]* href="((https?:\/\/)?([\w\-])+\.{1}domainurl\.([a-z]{2,6})([\/\w\.-]*)*\/?)">(.*?)</a>}' , $abcont , $allLinksMatchs);
有什么想法吗? , 非常感谢你
【问题讨论】:
-
那么您是否收到了一个 元素列表,每行一个?或者这些 元素是否也与其他元素一起嵌入在某些 HTML 代码中?
-
我怎么强调都不为过 (and nor can this post) 使用 RegEx 解析 HTML 是多么糟糕...考虑一下 using an XML/HTML parser
标签: php regex preg-match-all