Splitting strings in PHP and get last part
split($pattern,$string)
split strings within a given pattern or regex (it's deprecated since 5.3.0)
preg_split($pattern,$string)
split strings within a given regex pattern
explode($pattern,$string)
split strings within a given pattern
end($arr)
get last array element
So:
end(split('-',$str))
end(preg_split('/-/',$str))
$strArray = explode('-',$str)
$lastElement = end($strArray)
Will return the last element of a -
separated string.
And there's a hardcore way to do this:
$str = '1-2-3-4-5';
echo substr($str, strrpos($str, '-') + 1);
// | '--- get the last position of '-' and add 1(if don't substr will get '-' too)
// '----- get the last piece of string after the last occurrence of '-'
$string = 'abc-123-xyz-789';
$exploded = explode('-', $string);
echo end($exploded);
EDIT::Finally got around to removing the E_STRICT issue
Just check whether or not the delimiting character exists, and either split or don't:
if (strpos($potentiallyDelimitedString, '-') !== FALSE) {
found delimiter, so split
}