我正在尝试从文本中删除特定域和子域的所有超链接。
这是我正在尝试的
$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的所有domins +子域名,但是yahoo.com不会被删除。
我得到的输出只有第一个链接删除所有那里
请帮忙
答案 0 :(得分: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>
答案 1 :(得分: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;
答案 2 :(得分: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;
?>