Skip to content
Advertisement

What’s the most efficient test of whether a PHP string ends with another string?

The standard PHP way to test whether a string $str ends with a substring $test is:

JavaScript

Is this the fastest way?

Advertisement

Answer

What Assaf said is correct. There is a built in function in PHP to do exactly that.

JavaScript

If $test is longer than $str PHP will give a warning, so you need to check for that first.

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement