note 75042 deleted from function.substr by tularis
| From: | tularis@php.net | Date: | Mon, 31 Dec 2007 10:49:41 +0000 |
| Subject: | note 75042 deleted from function.substr by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-134616@lists.php.net to get a copy of this message | ||
Note Submitter: rasco at satx dot rr dot com
----
Very nice a dot vanderkolk at gmail dot com, just what I needed.
I have taken your function and added a bit to it, it will strip out ALL the chars after the dot
("."). It would be useful if someone could post how to only strip if they are zeros, and
round to 2 chars after the "." if they are not.
// Remove chars after . (200.000 => 200)
function removeDotZero($var)
{
$var = trim($var);
$pos = strpos($var,".");
if($pos != false)
{
$size = strlen($var);
for($i = $size; $i > $pos; $i--)
{
if(substr($var,-1,1) == "0" | "1" | "2" | "3" |
"4" | "5" | "6" | "7" | "8" | "9")
$var = substr($var,0,($i - 1));
else if(substr($var,-1,1) == ".")
{
$var = substr($var,0,($i - 1));
break;
}
else
break;
}
return $var;
}
else
return $var;
}
/*-------------------*/
// Example
$str = 200.000
$str = removeDotZero($str);
echo $str;
// Output
200