Program & Design Tips, tricks, tutorials, and tools on programming & web design

8May/090

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.

Filed under: PHP Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.