我想删除字符串中某些站点的URL
我用了这个:
<?php
$URLContent = '<p><a href="https://www.google.com/">Google</a></p><p><a href="https://www.anothersite.com/">AnotherSite</a></p>';
$LinksToRemove = array('google.com', 'yahoo.com', 'msn.com');
$LinksToCheck = in_array('google.com' , $LinksToRemove);
if (strpos($URLContent, $LinksToCheck) !== 0) {
$URLContent = preg_replace('#<a.*?>([^>]*)</a>#i', '$1', $URLContent);
}
echo $URLContent;
?>
In this example, I want to remove URLs of google.com, yahoo.com and msn.com websites only if any of them found in string $URLContent, but keep any other links.
上一个代码的结果是:
<p>Google</p><p>AnotherSite</p>
但我希望它是
<p>Google</p><p><a href="https://www.anothersite.com/">AnotherSite</a></p>
1条答案
按热度按时间xzlaal3s1#
一种解决方案是分解您的$URLContent并比较$LinksToCheck中的每个值。
可能是这样的: