Reputation: 1257
I am trying to Remove all hyper links of specific domain and sub domain from text..
Here is what I am trying
$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
Ok I just want to remove all domains+subdomains for my.com but yahoo.com should not be removed.
I am getting output of only 1st link removed rest all there
Upvotes: 1
Views: 643
Reputation: 3665
<?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;
Upvotes: 2
Reputation: 8741
This does a small job:
<?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>";
?>
Gives:
with www
example No www
Subdomain
<a href="http://www.yahoo.com/app/319354212" class="hh"> yahoo</a>
Upvotes: 3
Reputation: 1007
<?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;
?>
Upvotes: 1