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.