note 126767 added to function.strrchr
| From: | msegitpostpl at main dot php dot net | Date: | Sat, 15 Jan 2022 11:44:19 +0000 |
| Subject: | note 126767 added to function.strrchr | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-217495@lists.php.net to get a copy of this message | ||
After @jphansen, to extract portion of string (i.e. path), note that strrchr() returns false if
needle is not found.
You should use rather:
<?php
$path = '/www/public.htm/index.html';
$filename = substr(strrchr($path, "/"), 1) ?: (strrchr($path,
"/")==="/" ? "" : $path); /* code#1 */
// but NOT: $filename = substr(strrchr($path, "/"), 1); see below because
echo $filename; // "index.html"
// because:
$path = '/www/public.htm/index.html'; echo substr(strrchr($path, "/"), 1); //
"index.html" - OK.
$path = 'index.html'; echo substr(strrchr($path, "/"), 1); // "" -
WRONG, should be "index.html"
$path = '/www/public.htm/'; echo substr(strrchr($path, "/"), 1); // ""
- OK.
// Compare with basename() :
echo basename($path); // "public.htm"
// but
echo substr(strrchr($path, "/"), 1) ?: (strrchr($path, "/")==="/" ?
"" : $path); /* code#1 */ // ""
// BTW, to extract file extension with strrchr():
$extension = substr(strrchr(strrchr($path, '/') ?: $path, "."), 1);
// example, with note about pathinfo(), basename()
function ext($path) {
$ext = substr(strrchr(strrchr($path, '/') ?: $path, "."), 1);
// for demonstration:
$info = pathinfo($path);
echo str_pad($path, 30, ' '), ' ; ', str_pad($ext, 10, ' '),
' ; ', str_pad($info['extension'], 10, ' '),
' ; ', str_pad(substr(strrchr($path, "/"), 1) ?: (strrchr($path,
"/")==="/" ? "" : $path), 10, ' '), ' ; ',
basename($path), PHP_EOL;
return $ext;
}
// demonstration
echo 'String: ; ext(): ; p&ie: ; filename as code#1: ;
basename(): ', PHP_EOL, '( p&ie = pathinfo() and $info[\'extension\']
)', PHP_EOL;
ext('/www/public.htm/index.html');
ext('index.html');
ext('/www/public.htm/');
ext('/www/public.htm/index');
ext('index.');
?>
demonstration result:
String: ; ext(): ; p&ie: ; filename as code#1: ; basename():
( p&ie = pathinfo() and $info['extension'] )
/www/public.htm/index.html ; html ; html ; index.html ; index.html
index.html ; html ; html ; index.html ; index.html
/www/public.htm/ ; ; htm ; ; public.htm
/www/public.htm/index ; ; ; index ; index
index. ; ; ; index. ; index.
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.5.135 (proxied: 185.153.35.89)
----
Manual Page -- https://php.net/manual/en/function.strrchr.php
Edit -- https://main.php.net/note/edit/126767
Del: integrated -- https://main.php.net/note/delete/126767/integrated
Del: useless -- https://main.php.net/note/delete/126767/useless
Del: bad code -- https://main.php.net/note/delete/126767/bad+code
Del: spam -- https://main.php.net/note/delete/126767/spam
Del: non-english -- https://main.php.net/note/delete/126767/non-english
Del: in docs -- https://main.php.net/note/delete/126767/in+docs
Del: other reasons-- https://main.php.net/note/delete/126767
Reject -- https://main.php.net/note/reject/126767
Search -- https://main.php.net/manage/user-notes.php