[英]preg_replace regex for replacing anchor tag where URL is not sample.com
I have a regex for preg_replace which replace anchor tag where some URL(eg sample.com) exists in href.我有一个用于 preg_replace 的正则表达式,用于替换 href 中存在某些 URL(例如 sample.com)的锚标记。 Now what i need is a regex which will replace all other anchor tags which does not contain some URL(eg sample.com).
现在我需要的是一个正则表达式,它将替换所有其他不包含某些 URL(例如 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)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.