I have a regex for preg_replace which replace anchor tag where some URL(eg sample.com) exists in href. Now what i need is a regex which will replace all other anchor tags which does not contain some URL(eg sample.com).
Here is my regex:
$rw = "Go to hell";
$message = "<a href='http://twitter.com'>Twitter</a><br/><a href='http://google.com'>Google</a><br/><a href='http://facebook.com'>Facebook</a>";
$message = preg_replace("/<a[^>]*\bhref\s*=\s*'(?:[^']*google.com[^']*|[^][^']*facebook.com[^']*)'>(.*)<\/a>/siU", $rw, $message);
Result is:
Twitter (with link)
Go to hell
Go to hell
What i need:
Go to hell
Google (with link)
Facebook (with link)
Change your regex pattern as shown below(using lookahead negative assertion ?!
):
...
$message = preg_replace("/<a[^>]*\bhref\s*=\s*'https?:\/\/(www)?(?!(google\.com|facebook\.com))[^']*'>(.*)<\/a>/siU", $rw, $message);
print_r($message);
The output will be:
Go to hell
Google (with link)
Facebook (with link)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.