Reputation: 1827
I am using this function to convert square meters to square foot.
$sinput = rtrim(get_field('fl_area'), ", \t\n");
if(trim($sinput) == "0"){echo ' ' ;} else {$soutput = metersToSquareFeet($sinput); echo $soutput . ' sq. m (' . number_format($sinput ) . ' sq. f)' ;}
function metersToSquareFeet($meters, $echo = true)
{
$m = $meters;
$valInFeet = $m*10.7639;
$valFeet = (int)$valInFeet;
if($echo == true)
{
echo $valFeet;
} else {
return $valFeet;
}
}
Problem I have is with line:
rtrim(get_field('fl_area'), ", \t\n");
The user enters the number in the format 3,246
and i want to convert this to 3246
for my function to work.
Of course I could also modify the function somehow and not use rtrim
in the first place
Upvotes: 0
Views: 64
Reputation: 76636
You can use str_replace()
to remove all occurrences of commas and spaces from the string:
$m = str_replace(array(',',' '), '', $m);
Or even strtr()
:
$m = strtr($m, array(',' => '', ' ' => ''));
This is likely to be faster than regular expessions. However, if the number of function calls are minimal, the difference wouldn’t be noticeable.
Upvotes: 2
Reputation: 1804
You have a few options but probably your easiest one is to remove all non-numeric characters.
Not sure what your get_field does but assuming it just gets the field from the Input you could use regex like so.
$sinput = preg_replace( '/[^0-9]/', '', $get_field('fl_area') );
Also see: PHP regular expression - filter number only
Upvotes: 2
Reputation: 781761
rtrim
only removes characters from the end of the string, not the middle. Use preg_replace
:
$sinput = preg_replace('/[,\s]+/g', '', get_field('fl_area'));
Upvotes: 2