Multiple calls to preg_replace alters result
Posted
by Hurpe
on Stack Overflow
See other posts from Stack Overflow
or by Hurpe
Published on 2010-03-26T22:14:09Z
Indexed on
2010/03/26
22:23 UTC
Read the original article
Hit count: 312
I have a bunch of files that were named in a somewhat standard format. The standard form is basically this:
[integer]_word1_word2_word3_ ... _wordn where a word could really be anything, but all words are separated by an underscore.
There is really only 3 things I want to do to the text:
1.) I want to modify the integer, which is always at the beginning, so that something like "200" would become $ 200.00.
2.) replace any "words" of the form "with", "With", "w/", or "W/" with "with".
3.) Replace all underscores with a space.
I wrote three different preg_replace calls to do the trick. They are as follows:
1.) $filename = preg_replace("/(^[0-9]+)/","$ $1.00",$filename)
2.) $filename = preg_replace("/_([wW]|[wW]ith)_/"," with ",$filename)
3.) $filename = preg_replace("/_/"," ",$filename);
Each replacement works as expected when run individually, but when all three are run, the 2nd replacement is ignored. Why would something like that occur?
Thanks for the help!
© Stack Overflow or respective owner