一个人用什么正则表达式从字符串中删除域?我尝试了几种组合,但我想出的壁橱将多次发现匹配项后将所有内容移除。
$string = 'The quick brown fox <img src="http://domain.com/images/fox.jpg"> jumps over the lazy dog.';
preg_replace('/http:\/\/(.*)domain.com/', '', $string);`
Run Code Online (Sandbox Code Playgroud)
寻找检测并删除img和href src中的以下组合www.domain.com,domain.com和subs.domain.com。
你.*很贪心 它会消耗尽可能多的字符来满足匹配要求。放一个?使其不贪心,如下所示:
preg_replace('/http:\/\/(.*?)domain\.com/', '', $string);
Run Code Online (Sandbox Code Playgroud)