简体   繁体   English

Preg_replace不替换所有匹配项

[英]Preg_replace do not replace all matches

I am trying to replace every link in a file with regular expression but when i try it, it replaces only the first match and continue forward without replacing the others. 我试图用正则表达式替换文件中的每个链接,但是当我尝试使用它时,它仅替换第一个匹配项并继续前进而不替换其他匹配项。 This is my code: 这是我的代码:

$allData = preg_replace( '|(.+?src=")(.+?)(".*)|is', "\\1" . $urlRoot . "/\\2\\3", $allData );

it has to take the 2nd match from the 3 and place it after the $urlRoot and do this for every match in the file. 它必须从3中获得第二个匹配项,并将其放置在$urlRoot ,并针对文件中的每个匹配项执行此操作。 It is working but only for the first one. 它正在工作,但仅适用于第一个。

Just remove the .* from 3rd group: 只需从第三组中删除.*

$allData = preg_replace( '|(.+?src=")(.+?)(")|is', "\\1" . $urlRoot . "/\\2\\3", $allData );

You could aloso do: 您可以做:

$allData = preg_replace( '|\bsrc="([^"]+)|is', 'src="' . $urlRoot . "/$1", $allData );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM