Forum Moderators: coopster

Message Too Old, No Replies

Regular Expression Problem in PHP

"/\$/" Grabbing $95 instead of just $

         

Yardboy

4:57 am on Mar 2, 2004 (gmt 0)

10+ Year Member



$text = preg_replace("/\$/","",$text);

is turning:
4 in. ...$12.64
into
4 in. ....64

im trying to write an expression that will match:

...add $[either 9879.09,98 - dollars with or without cecimal and cents]

or

...$[either 9879.09,98 - dollars with or without cecimal and cents]

i can get it to match on regular expression tools online, but when i call it in PHP (4.3.4) on the server, its does things like the above example. any ideas?

jonknee

5:50 am on Mar 2, 2004 (gmt 0)

10+ Year Member



PHP's built in money_format() [us4.php.net] might help you here... It's slick.

Yardboy

4:06 pm on Mar 2, 2004 (gmt 0)

10+ Year Member



unfortunately, that won't do it. i'm trying to hack a string off the back end of a field coming out of a mysql db. i need a reg. expression.

ergophobe

4:22 pm on Mar 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Can I ask for some clarification? If I understand a line of data looks like:

a whole bunch of stuff with add $[either 9879.09,98 - dollars with or without cecimal and cents] in the middle

What is that number you give in the example? It's neither a european or an american number format. Do you mean "1.987.909,98" (in other words, European format for large numbers and decimal places)?

Is the "$[either " always present? I'm not sure whether this *is* your data or *describes* your data.

coopster

5:14 pm on Mar 2, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The dollar sign is a special regular expression character and you need to escape it as you no doubt know and we see in your expression. However, you also need to escape the escape character within double quotes:
$text = preg_replace("/\\\$/","",$text);

Optionally, you can use single quotes to stop the PHP parser from interpolating the variable:
$text = preg_replace('/\$/',"",$text);

I've seen plenty of explanations, but nothing definitive. The best yet comes from the MySQL manual [mysql.com] of all places:

Note: Because MySQL uses the C escape syntax in strings (for example, `\n'), you must double any `\' that you use in your LIKE strings. For example, to search for `\n', specify it as `\\n'. To search for `\', specify it as `\\\\' (the backslashes are stripped once by the parser and another time when the pattern match is done, leaving a single backslash to be matched).

Yeah, this is confusing stuff. Other explanations I've seen in the preg_replace [php.net] User Contributed notes:

...The reason for this is that before the regex engine can interpret..., PHP interprets it. Thus, you have to escape your backslashes twice: once for PHP, and once for the regex engine.

...and there are yet even more in the ereg_replace [php.net] User Contributed notes. I can't tell you if that's how it works or not, but I haven't found a better explanation [webmasterworld.com] yet.

Yardboy

2:45 am on Mar 3, 2004 (gmt 0)

10+ Year Member



i love webmasterworld :D

ergophobe: i was just using an example, as in a currency string with decimal place and cents, or currency without decimal place and cents. like $9080.93 or $98.

coopster, thank you. i spent literally 12 hrs on this. i'm not very smart, hehe. actually, i read as much as i could about regular expressions online, and am starting to get the hang of them, but this was causing me alot of grief.

coopster

1:00 pm on Mar 3, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You're welcome.

If you've spent hours reading, researching and learning, then I would say that you are very smart. Regular expressions are powerful and take a bit of time to learn. Keep at it!