May
28
2010

I Hate You preg_replace()!

Friday, May 28, 2010 - 07:56:43 pm
(Posted Under: Development, PHP)
Well, this is just a plain annoying PHP gotcha...

Last year I ported some Perl code to PHP. A quite literally port as it would seem. Since then I've had some problems with dollar amounts going haywire by the time they get to the browser.

Just tracked down the problem - when digits follow a $ in a replacement string fed to preg_replace(), they're treated as back references. How annoying (I don't think this was true in Perl - at least I never noticed a problem)!

Since I am doing a preg_match() before the replace anyway, I've swapped to str_replace() and everything is copacetic, Generally speaking, I feel like I use preg_replace() a lot, so this is a rather disturbing revelation.
Now Playing: Beat Angels - Grow Up

Post Comment



All fields are required. Email addresses will not be published, but are required for anti-spam purposas.

Switch Styles

About Style Switching.

!Weblog Index

Apr May 2010 Jun
SU MO TU WE TH FR SA
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31

Categories

RSS FeedRSS Feed