php - Get X first/last WORDS from string -


ok, need...

example input :

$str = "well, guess know # : & ball";

example output :

  • firstwords($str,5) should return array("well","i","guess","i","know")
  • lastwords($str,5) should return array("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

Popular posts from this blog

php - Calling a template part from a post -

Firefox SVG shape not printing when it has stroke -

How to mention the localhost in android -