note 119982 added to function.substr-compare
| From: | bishop@php.net | Date: | Mon, 03 Oct 2016 19:28:35 +0000 |
| Subject: | note 119982 added to function.substr-compare | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-206740@lists.php.net to get a copy of this message | ||
This function efficiently implements checks for strings beginning or ending with other strings:
<?php
function str_begins($haystack, $needle) {
return 0 === substr_compare($haystack, $needle, 0, strlen($needle));
}
function str_ends($haystack, $needle) {
return 0 === substr_compare($haystack, $needle, -strlen($needle));
}
var_dump(str_begins('http://example.com', 'https://'));
?>
Note that these are not multi-byte character set aware.
----
Server IP: 72.52.91.14
Probable Submitter: 107.15.255.60
----
Manual Page -- http://php.net/manual/en/function.substr-compare.php
Edit -- https://master.php.net/note/edit/119982
Del: integrated -- https://master.php.net/note/delete/119982/integrated
Del: useless -- https://master.php.net/note/delete/119982/useless
Del: bad code -- https://master.php.net/note/delete/119982/bad+code
Del: spam -- https://master.php.net/note/delete/119982/spam
Del: non-english -- https://master.php.net/note/delete/119982/non-english
Del: in docs -- https://master.php.net/note/delete/119982/in+docs
Del: other reasons-- https://master.php.net/note/delete/119982
Reject -- https://master.php.net/note/reject/119982
Search -- https://master.php.net/manage/user-notes.php