这是一个不是我的脚本代码,我试着修改它。它会搜索所有的标签,然后删除它们。您将如何修改代码以只擦除给定域或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++;
}
}我试过这样做,但对我没有用:
Regex:
使用preg_match_all在搜索中指定
preg_match_all('{<a.*?[^>]* href="((https?:\/\/)?([\w\-])+\.{1}domainurl\.([a-z]{2,6})([\/\w\.-]*)*\/?)">(.*?)</a>}' , $abcont , $allLinksMatchs);有什么主意吗?,我非常感谢你
发布于 2019-03-05 18:17:24
我没有像您建议的那样尝试和使用用正则表达式解析HTML,而是选择使用DOMDocument类。
function remove_domain($str, $domainsToRemove)
{
$domainsToRemove = is_array($domainsToRemove) ? $domainsToRemove : array_slice(func_get_args(), 1);
$dom = new DOMDocument;
$dom->loadHTML("<div>{$str}</div>", LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
$anchors = $dom->getElementsByTagName('a');
// Code taken and modified from: http://php.net/manual/en/domnode.replacechild.php#50500
$i = $anchors->length - 1;
while ($i > -1) {
$anchor = $anchors->item($i);
foreach ($domainsToRemove as $domain) {
if (strpos($anchor->getAttribute('href'), $domain) !== false) {
// $new = $dom->createElement('p', $anchor->textContent);
$new = $dom->createTextNode($anchor->textContent);
$anchor->parentNode->replaceChild($new, $anchor);
}
}
$i--;
}
// Create HTML string, then remove the wrapping div.
$html = $dom->saveHTML();
$html = substr($html, 5, strlen($html) - (strlen('</div>') + 1) - strlen('<div>'));
return $html;
}然后,您可以在以下示例中使用上述代码。
请注意,您可以将字符串作为要删除的域传递,也可以传递一个域数组,或者可以利用func_get_args并传递无限数量的参数。
$str = <<<str
<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>
str;
// Example usage
remove_domain($str, 'domainurl.com');
remove_domain($str, 'domainurl.com', 'googlead.com');
remove_domain($str, ['domainurl.com', 'googlead.com']);首先,我将您的字符串存储在一个变量中,但这样我就可以将它用于回答;将$str替换为您从何处获得的代码。
loadHTML函数接受一个HTML,但需要一个子元素--因此我将字符串包装在div中。
while循环将遍历锚元素,然后用锚标记的内容替换任何匹配指定域的内容。
注意,我在这行上面留下了一个注释,你可以用它来代替。这将用p标记替换锚元素,该标记具有默认的display: block;样式,这意味着布局不会中断。但是,由于您的预期输出只是文本节点,所以我将其保留为一个选项。
发布于 2019-03-05 16:33:12
那麽:
<a.*? href=\".*www\.googlead\.com.*\">(.*?)<\/a>因此,它变成:
preg_match_all('{<a.*? href=\".*www\.googlead\.com.*\">(.*?)<\/a>}' , $abcont , $allLinksMatchs);这只会从a中移除www.googlead.com标记。
您可以检查regex结果这里。
发布于 2019-03-05 16:32:39
假设HTML包含在以下变量中。
使用preg_replace应该是一个更好的选择,下面的函数应该对您有所帮助:
function removeLinkTagsOfDomain($html, $domain) {
// Escape all regex special characters
$domain = preg_quote($domain);
// Search for <a> tags with a href attribute containing the specified domain
$pattern = '/<a .*href=".*' . $domain . '.*".*>(.+)<\/a>/';
// Final replacement (should be the text node of <a> tags)
$replacer = '$1';
return preg_replace($pattern, '$1', $html);
}
// Usage:
$domains = [...];
$html = '...';
foreach ($domains as $d) {
$html = removeLinkTagsOfDomain($html, $d);
}https://stackoverflow.com/questions/55007093
复制相似问题