PHP删除特定域和子域的所有超链接


PHP Remove all hyperlinks of specific domain and subdomain

我正在尝试从文本中删除特定域和子域的所有超链接
以下是我正在尝试的

$link = '<a href="http://www.my.com/app/319354212" class="hh"> with www </a> 
        example <a href="http://my.com/app/319354212" class="hh"> No www </a>
        <a href="http://www.subdomain.my.com/app/319354212" class="hh"> Subdomain</a>
        <a href="http://www.yahoo.com/app/319354212" class="hh"> yahoo</a> ';
$pattern1 = '|<a [^>]*href="http://www.my.com(.*)">(.*)</a>|iU';
$str = preg_replace($pattern1, "''2", $link);
 echo $str

好的,我只想删除my.com的所有域+子域,但雅虎不应该被删除
我得到的输出只有第一个链接删除了其余所有的

这做了一个小工作:

<?php
$link = '<a href="http://www.my.com/app/319354212" class="hh"> with www </a> 
        example <a href="http://my.com/app/319354212" class="hh"> No www </a>
        <a href="http://www.subdomain.my.com/app/319354212" class="hh"> Subdomain</a>
        <a href="http://www.yahoo.com/app/319354212" class="hh"> yahoo</a> ';
$pattern1 = '|<a href="http://(www'..*)?my.com(.*)">(.*)</a>|iU';
$str = preg_replace($pattern1, "''3", $link);
echo "<textarea style='"width:700px; height:90px;'">"
  . $str
  . "</textarea>";
?>

提供:

with www  
    example  No www 
     Subdomain
    <a href="http://www.yahoo.com/app/319354212" class="hh"> yahoo</a> 
<?php
$link = '<a href="http://www.my.com/app/319354212" class="hh"> with www </a> 
    example <a href="http://my.com/app/319354212" class="hh"> No www </a>
    <a href="http://www.subdomain.my.com/app/319354212" class="hh"> Subdomain</a>
    <a href="http://www.yahoo.com/app/319354212" class="hh"> yahoo</a> ';
$pattern1 = '~([<]a's+href="http':'/'/)([a-zA-Z0-9]+'.)*(my'.com)([^>]*["][>])(.*)(</a>)~i';
// I'm not sure if you want to keep the text or not, but if you do not
// want to keep it, remove $3 from the next line (so it's now '' instead):
$replacement = '$3';
$str = preg_replace($pattern1, $replacement, $link);
echo $str;
<?php
$link = '<a href="http://www.my.com/app/319354212" class="hh"> with www </a> 
        example <a href="http://my.com/app/319354212" class="hh"> No www </a>
        <a href="http://www.subdomain.my.com/app/319354212" class="hh"> Subdomain</a>
        <a href="http://www.yahoo.com/app/319354212" class="hh"> yahoo</a> ';
$replacement_string = "replacement string";
$new_url = preg_replace('/<a href's*='s*('"|'')(http':'/'/www'.|http':'/'/).*my'.com.*>.*<'/a>/', '', $link);
print $new_url;
?>