ConFoo Montreal 2026: Call for Papers

Voting

: max(eight, 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', 'https://'));

?>

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

<< Back to user notes page

To Top