note 115277 deleted from function.substr by mazzanet
| From: | mazzanet@php.net | Date: | Tue, 01 Jul 2014 07:27:15 +0000 |
| Subject: | note 115277 deleted from function.substr by mazzanet | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-199790@lists.php.net to get a copy of this message | ||
Note Submitter: thflori plus php at gmail dot com
----
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