Help with regex replace in php -
I have a bunch of URLs in static HTML files, which must be changed.
It is now:
& lt; Img src = "/ foldera / folderb / folderc / images / imgxyz.jpg" />
They should look like this:
& lt; Img src = "imgxyz.jpg" />
So, I have just written a php script which opens to each and a preg_replace () does.
My regex (with double-sided backslash, yes):
$ regex = '/ & lt; Img src = "\\ / foldera \\ / folderb \\ / folderc \\ / images \\ / ([^"] *) "\\ /> / '$ replacement =' & lt; img src =" $ 0 "/ & gt;;
That's why I'm capturing only a few images / images until the ending quote.
But whatever I It is something like this:
"/>
It seems that the capture group is redundant and ... or something
for replacement $ 1
. $ 0
matches the whole pattern. You want the first group.
$ replacement = '
An even better way to use basename
as part of your replacement Will be:
$ regex = '/ (& lt; img src = ") ([^"] *) "(\\ / & gt;) / E '; $ Replacement = "strips slash ('\ $ 1'). Basnem (strips slash ('\ $ 2')). StripsSlabs ('\ $ 3')";
Comments
Post a Comment