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

问题描述 投票:1回答:3

我正在尝试从文本中删除特定域和子域的所有超级链接。这是我正在尝试的]

$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的所有域+子域,但不应删除yahoo.com。我只删除了第一个链接的输出,剩下的全部]

php preg-replace
3个回答
3
投票

这做得很少:

<?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> 

2
投票
<?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;

1
投票
<?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;

?>
© www.soinside.com 2019 - 2024. All rights reserved.