The alternative would be a combination of substr, index and either decode or replace_substr that is not nearly as compact and readable. The above also gracefully handles having no final word, which would require more custom code the other way.
A) Congratulations on resurrecting an 8+ year old thread! =)
B) That was not part of the original request.
C) Custom function is probably the best route. Outside of that you could probably use regex_replace, which did not exist when this question was posted.
Here ya go. Theres no logical reason why you would want to trim delimiters in this function.
$turnString = $inputString;
$curIdx = 1;
$retWord = '';
while($curIdx <= $wordNumber and $retWord = '')
begin
if($curIdx = $wordNumber)
begin
if(substr($turnString,1,length($delimiter)) = $delimiter)
begin
$retWord = null;
end
else
begin
if(index( $turnString ,$delimiter,1) > 0)
begin
$retWord = substr( $turnString,1,index( $turnString ,$delimiter,1) - 1);
end
else
begin
$retWord = $turnString;
end
end
end
else
begin
$turnString = substr($turnString,index( $turnString,$delimiter,1) + length($delimiter),10000);
$curIdx = $curIdx + 1;
end
end
return($retWord);