[英]Replace all urls in string not matching pattern in php
I'm using this code to replace urls in a string 我正在使用此代码替换字符串中的网址
preg_replace('#<a.*?>(.*?)</a>#i', '\1', $text)
How do I do the same thing, but keeping urls that match a certain pattern (ie start with a domain I want to keep)? 我该如何做同样的事情,但要保留与特定模式匹配的网址(即以我要保留的域名开头)?
Update 更新
Turns out that the urls I want to keep include relative urls, so I now want to eliminate all urls that don't match the given url pattern and are not relative links. 原来,我要保留的URL包括相对URL,因此我现在要消除所有与给定URL模式不匹配且不是相对链接的URL。
You need a negative look-ahead assertion: 您需要一个否定的前瞻断言:
preg_replace('#<a(?![^>]+?href="?http://keepthisdomain.com/foo/bar"?).*?>(.*?)</a>#i', '\1', $text);
Edit: If you want to match only relative domains, the logic is the same. 编辑:如果只想匹配相对域,则逻辑是相同的。 Just take out the protocol and domain name: 只需取出协议和域名即可:
preg_replace('#<a(?![^>]+?href="?/?path/to/foo/bar"?).*?>(.*?)</a>#i', '\1', $text);
The ?
?
after "
and /
means that those characters are optional. So, the second example will work for either path/to/foo/bar
or /path/to/foo/bar
. "
和/
表示这些字符是可选的。因此,第二个示例适用于path/to/foo/bar
或/path/to/foo/bar
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.