note 115277 added to function.substr
| From: | thfloriplusphpatgmaildotcom | Date: | Thu, 26 Jun 2014 14:44:56 +0000 |
| Subject: | note 115277 added to function.substr | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-199768@lists.php.net to get a copy of this message | ||
For me it seems a bit too lazy to accept false and NULL to return an empty string. What if a
function (like substr) can't find the string?
So use this function to wrap substr:
<?php
function mysubstr($s, $start, $end) {
if (!is_int($end)) {
return substr($s, $start);
} else {
return substr($s, $start, $end);
}
}
$uri = 'path/to/something';
echo substr($uri, 0, strpos($uri, '/', 1)); // path
echo mysubstr($uri, 0, strpos($uri, '/', 1)); // path
$uri = 'something';
echo substr($uri, 0, strpos($uri, '/', 1)); // (empty string)
echo mysubstr($uri, 0, strpos($uri, '/', 1)); // something
----
Server IP: 72.52.91.14
Probable Submitter: 176.94.130.178
----
Manual Page -- http://php.net/manual/en/function.substr.php
Edit -- https://master.php.net/note/edit/115277
Del: integrated -- https://master.php.net/note/delete/115277/integrated
Del: useless -- https://master.php.net/note/delete/115277/useless
Del: bad code -- https://master.php.net/note/delete/115277/bad+code
Del: spam -- https://master.php.net/note/delete/115277/spam
Del: non-english -- https://master.php.net/note/delete/115277/non-english
Del: in docs -- https://master.php.net/note/delete/115277/in+docs
Del: other reasons-- https://master.php.net/note/delete/115277
Reject -- https://master.php.net/note/reject/115277
Search -- https://master.php.net/manage/user-notes.php