I have a form for people to submit the cost of an item. I need to check for ,.. then remove the $ if they put it in with their cost. I have tried everything.
Even $NewCost = ereg_replace ("\$", "", $Cost); doesnt work I thought that with the escape char it would, but no luck.
Any help or insight would be hugely appriciated.
how about:
$NewCost = ereg_replace ("$", "\$", $Cost);
?
nope that gives me $300$
cane u paste the code?
this is a shiter, and a slight hack route, byut try this.
$NewCost = ereg_replace ("\\$", "\$", $Cost);
this is cause you need to escape the slash, your passing a string, before you can escape the $ sign.
I had this same problem. You need to approach the problem differently. Instead of REMOVING what you don't want, parse the entered string and copy just what you WANT to a new variable i.e only take numeric 0 thru 9 and decimal point. That effectively gets rid of dollar sign, commas and handles the decimal for you. Let me know if you require a code snippet. This method is used in many applications where monetary values are enterted in a text field.
You can also modify this approach slightly for percent input, counts etc.
Chris
How about using str_replace instead?
$p = "$123.00"; $q = str_replace("$","",$p); echo "Was $p, but now is $q";
this works well, the main problem with ereg is $ is also a special chariter.
$q = str_replace("$","\$",$p);
would work even better!
$q = preg_replace(\'/\$/\',\'\$\',$p);
$q = preg_replace('/\$/','\$',$p);