PHP 8.5.0 Beta 2 available for testing

Voting

: nine plus zero?
(Example: nine)

The Note You're Voting On

bishop at php dot net
8 years ago
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', 'http://'));

?>

Note that these are not multi-byte character set aware.

<< Back to user notes page

To Top