I have a bunch of urls in static html files which need to be changed.
They look like this now:
<img src="/foldera/folderb/folderc/images/imgxyz.jpg" />
They need to look like this:
<img src="imgxyz.jpg" />
So, I just wrote a php script that opens each and does a preg_replace().
My regex (with the double escaped backslashes, yes):
$regex = '/<img src="\\/foldera\\/folderb\\/folderc\\/images\\/([^"]*)" \\/>/'
$replacement = '<img src="$0" />' ;
So I am only capturing anything after /images until the closing quote.
But what I get is something like:
<img src="<img src="/foldera/folderb/folderc/images/imgxyz.jpg" />" />
It seems the capture group is overzealous and … or something is not matching with the /foldera/folderb part.
What is going on here?
Use
$1for the replacement.$0matches the whole pattern. You want the first group.An even better way would be to use
basenameas part of your replacement: