Click here to Skip to main content
11,437,397 members (34,638 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: HTML PHP
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 3-Sep-12 5:50am
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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...
  Permalink  
v3
Comments
thomasriley at 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 Murt at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Adding with Joan, You can extract the number part only from your string.

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

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



Advertise | Privacy | Mobile
Web03 | 2.8.150428.2 | Last Updated 4 Sep 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100