PHP string ends with

There’s a hundred ways to do this, but here’s the one I came up with. It doesn’t use regex’s so it should be pretty quick.

function ends_with($str, $suffix) {
    return substr($str, -strlen($suffix)) == $suffix;

function starts_with($str, $prefix) {
    return substr($str, 0, strlen($prefix)) == $prefix;

For completeness, I added the corresponding starts_with function.

Posted in

Leave a comment

Leave a Reply

Your email address will not be published. Required fields are marked *