tags:

views:

55

answers:

2

Hello!

The following code returns everything to right of the last occurrence of a dash:

$string1 = 'some-random-string-123456';
$string2 = strrchr($string1, '-');

echo $string2;

output: -123456

is there a function that will return everything to the left of the last dash? so the out put would be:

some-random-string

Thanks!

+4  A: 
$string2 = substr($string1, 0, strrpos($string1, "-"))
Nicolás
Removed my answer as this is slightly more elegant. +1
Dereleased
Wasn't reading the question properly. This is a fine answer.
Gordon
A: 

Something like this will work..

$string2 = substr($string1, 0, strpos($string1, '-'));
Ian P
This outputs "some", not what was asked
Nicolás