删除特定 URL 域的标签 <a> php

remove tags <a> to a specific URL domain php

这不是我的脚本代码,我尝试修改它。它的作用是搜索所有标签,然后将其删除。您将如何修改代码以仅删除给定域或 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 包含在以下变量中。

使用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 = '';

    return preg_replace($pattern, '', $html);
}

// Usage:

$domains = [...];
$html = '...';

foreach ($domains as $d) {
    $html = removeLinkTagsOfDomain($html, $d);
}

怎么样:

<a.*? href=\".*www\.googlead\.com.*\">(.*?)<\/a>

所以变成:

preg_match_all('{<a.*? href=\".*www\.googlead\.com.*\">(.*?)<\/a>}' , $abcont , $allLinksMatchs);

这只会从 www.googlead.com 中删除 a 个标签。

您可以检查正则表达式结果 here

我没有像您建议的那样尝试 parse HTML with regular expressions,而是选择使用 DOMDocument class。

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;,这意味着您的布局不太可能被破坏。但是,由于您的预期输出只是文本节点,因此我将其保留为一个选项。

Live demo