note 96202 deleted from function.substr by thiago
| From: | thiago@php.net | Date: | Mon, 15 Feb 2010 01:59:22 +0000 |
| Subject: | note 96202 deleted from function.substr by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-165953@lists.php.net to get a copy of this message | ||
Note Submitter: Mario Wenzel
----
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