php - Get the last word of a string -
i have tried few things last part out done this:
$string = 'sim-only 500 | internet 2500'; preg_replace("sim-only ^([1-9]|[1-9][0-9]|[1-9][0-9][0-9][0-9])$ | internet ","",$string , preg_match("/[^ ]*$/","",{abo_type[1]})
the first 1 wont work , second returns array realy need string.
if you're after last word in sentence, why not this?
$string = 'sim-only 500 | internet 2500'; $pieces = explode(' ', $string); $last_word = array_pop($pieces); echo $last_word;
i wouldn't recommend using regular expressions it's unnecessary, unless want reason.
$string = 'retrieving last word of string using php.'; preg_match('/[^ ]*$/', $string, $results); $last_word = $results[0]; // $last_word = php.
the substr()
method gave abit better
$string = 'retrieving last word of string using php.'; $last_word_start = strrpos($string, ' ') + 1; // +1 don't include space in our result $last_word = substr($string, $last_word_start); // $last_word = php.
it faster, although doesn't make of difference on things this. if you're needing know last word on 100,000 word string, should going in different way.
Comments
Post a Comment