note 75042 added to function.substr
| From: | rascoatsatxdotrrdotcom at osu1 dot php dot net | Date: | Wed, 09 May 2007 21:31:38 +0000 |
| Subject: | note 75042 added to function.substr | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-126369@lists.php.net to get a copy of this message | ||
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
----
Server IP: 69.39.81.135
Probable Submitter: 24.155.186.4 (proxied: 1.1 TSSBSR2)
----
Manual Page -- http://www.php.net/manual/en/function.substr.php
Edit -- https://master.php.net/note/edit/75042
Del: integrated -- https://master.php.net/note/delete/75042/integrated
Del: useless -- https://master.php.net/note/delete/75042/useless
Del: bad code -- https://master.php.net/note/delete/75042/bad+code
Del: spam -- https://master.php.net/note/delete/75042/spam
Del: non-english -- https://master.php.net/note/delete/75042/non-english
Del: in docs -- https://master.php.net/note/delete/75042/in+docs
Del: other reasons-- https://master.php.net/note/delete/75042
Reject -- https://master.php.net/note/reject/75042
Search -- https://master.php.net/manage/user-notes.php