php - Get X first/last WORDS from string -
ok, need...
example input :
$str = "well, guess know # : & ball";
example output :
firstwords($str,5)
should returnarray("well","i","guess","i","know")
lastwords($str,5)
should returnarray("is","it","is","a","ball")
i've tried custom regexes , str_word_count
, still feel if i'm missing something.
any ideas?
all need
$str = "well, guess know # : & ball"; $words = str_word_count($str, 1); $firstwords = array_slice($words, 0,5); $lastwords = array_slice($words, -5,5); print_r($firstwords); print_r($lastwords);
output
array ( [0] => [1] => [2] => guess [3] => [4] => know ) array ( [0] => [1] => [2] => [3] => [4] => ball )
Comments
Post a Comment