PHP: equivalent of MySQL's function SUBSTRING_INDEX ?

Viewed 8652

I love the SUBSTRING_INDEX function in MySQL, especially because you can use negative indexes to start searching from the right side of the string.

Is there an equivalent of this function in PHP? (or an easy way to do it with a bit of code)

5 Answers

If you need equivalent only for SUBSTRING_INDEX(str, delim, 1), you can use:

list($str,) = explode($delim, $str);
Related