note 84899 deleted from function.str-pad by danbrown
| From: | danbrown@php.net | Date: | Tue, 05 Aug 2008 14:00:28 +0000 |
| Subject: | note 84899 deleted from function.str-pad by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-143160@lists.php.net to get a copy of this message | ||
Note Submitter: sony suwanamiharja
----
// @function : str_pad_clear()
// @email : sonsonz@gmail.com
$string = "00001010"; // get 1010
$str = str_pad_clear($string,"0",STR_PAD_LEFT); // result 1010
echo $str;
function str_pad_clear($string, $char, $type = STR_PAD_LEFT) {
$strlen = strlen($string);
$pos = 0;
for ($x=0;$x<$strlen;$x++) {
if ($type == 'STR_PAD_LEFT') {
if (round(strpos($string,$char,$x),0) != round($x,0)) {
if ($pos == 0) {
$pos = $x;
continue;
}
}
} else {
//'STR_PAD_RIGHT'
if (strpos($string,$char,($x * -1)) != $x) {
if ($pos == 0) {
$pos = $x;
continue;
}
}
}
}
$str = substr($string,$pos);
return $str;
}