note 96202 added to function.substr
| From: | MarioWenzel at osu1 dot php dot net | Date: | Sun, 14 Feb 2010 12:33:08 +0000 |
| Subject: | note 96202 added to function.substr | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-165949@lists.php.net to get a copy of this message | ||
One has to note that substr() always returns false on an empty string.
The line "If string is less than or equal to start characters long, FALSE will be
returned." is a little bit misleading since start can be negative.
substr('',-1) returns false as opposed to an empty string. For positive starts the
documentation is consistent.
I cannot stretch enough that substr() returns false on an empty string. The documentation does not
state that explicitly. So if you're doing something like this:
<?PHP
function extract_numbers($input_text) {
while (($char = substr($input_text, 0, 1)) !== false) {
if (is_numeric($char)) {
$result .= $char;
}
$input_text = substr_replace($input_text, '', 0 , 1);
}
return $result;
}
?>
be sure to check on false and not on an empty string
----
Server IP: 212.124.37.9
Probable Submitter: 92.78.0.203
----
Manual Page -- http://www.php.net/manual/en/function.substr.php
Edit -- https://master.php.net/note/edit/96202
Del: integrated -- https://master.php.net/note/delete/96202/integrated
Del: useless -- https://master.php.net/note/delete/96202/useless
Del: bad code -- https://master.php.net/note/delete/96202/bad+code
Del: spam -- https://master.php.net/note/delete/96202/spam
Del: non-english -- https://master.php.net/note/delete/96202/non-english
Del: in docs -- https://master.php.net/note/delete/96202/in+docs
Del: other reasons-- https://master.php.net/note/delete/96202
Reject -- https://master.php.net/note/reject/96202
Search -- https://master.php.net/manage/user-notes.php