note 79766 added to function.intval
| From: | michieledthalentnl at osu1 dot php dot net | Date: | Wed, 12 Dec 2007 01:44:28 +0000 |
| Subject: | note 79766 added to function.intval | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-133378@lists.php.net to get a copy of this message | ||
A function to get an integer from everywhere in a string (concatenated or not).
<?php
function str2int($string, $concat = true) {
$length = strlen($string);
for ($i = 0, $int = '', $concat_flag = true; $i < $length; $i++) {
if (is_numeric($string[$i]) && $concat_flag) {
$int .= $string[$i];
} elseif(!$concat && $concat_flag && strlen($int) > 0) {
$concat_flag = false;
}
}
return (int) $int;
}
echo var_dump(str2int('sh12apen11')); // int(12)
echo var_dump(str2int('sh12apen11', false)); // int(1211)
echo var_dump(str2int('shap99en')); // int(99)
echo var_dump(intval('shap99en')); // int(0)
?>
----
Server IP: 83.137.20.135
Probable Submitter: 212.129.219.171
----
Manual Page -- http://www.php.net/manual/en/function.intval.php
Edit -- https://master.php.net/note/edit/79766
Del: integrated -- https://master.php.net/note/delete/79766/integrated
Del: useless -- https://master.php.net/note/delete/79766/useless
Del: bad code -- https://master.php.net/note/delete/79766/bad+code
Del: spam -- https://master.php.net/note/delete/79766/spam
Del: non-english -- https://master.php.net/note/delete/79766/non-english
Del: in docs -- https://master.php.net/note/delete/79766/in+docs
Del: other reasons-- https://master.php.net/note/delete/79766
Reject -- https://master.php.net/note/reject/79766
Search -- https://master.php.net/manage/user-notes.php