Click here to Skip to main content
15,894,410 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi everyone,

I have a form on my HTML document that requires the user to type in a PRICE for an item, and it is POSTED by PHP.

The format of the string that is being posted is: £0.00, or £00.00, £000.00 etc.

Basically I'm trying to strip away the Pound Sign but keep the decimal place, so that it goes in my sql database OK.

Is there an easy way around this? I tried using mysql_real_escape_string and str_replace but it doesnt want to work for me!!

Any comments and answers are greatly appreciated.

Regards, Tom.
Posted

If the string has always that format, then take a look at this:

http://php.net/manual/en/function.substr.php[^]

With this kind of operation you could remove the first char always in any string...

If the string doesn't has always that format, then take a look at this:

If http://php.net/manual/en/function.str-replace.php[^] is not working for you...

Then you could look at regular expressions:

http://php.net/manual/en/function.preg-match.php[^]

http://weblogtoolscollection.com/regex/regex.php[^]

hope this helps...
 
Share this answer
 
v3
Comments
thomasriley 3-Sep-12 11:14am    
Hi Joan, if the string is not always in that format, do you have any suggestions? For example, if I could search for the Pound Sign first, and if it exists, get rid of it, if it doesn't exist, then save the string how it is?
Joan M 3-Sep-12 11:20am    
Sorry badly explained, the first is only the one that's affected by the format, the others are precisely independent on the format... Take a look at them I'm sure that one or another will work for you.
Adding with Joan, You can extract the number part only from your string.

C++
$string='\$123.33';
$num="";
for($i=0;$i<strlen($string);$i++)>
{
 if(is_numeric($string[$i])==true || $string[$i]=='.' || || $string[$i]=='-')
  $num.=$string[$i];
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900