I would like to know what method is good to get the first 20 words from an english sentence. I need to analyze its subject and verb before processing further. Because there are sentences that are useless so I don’t need to analyze them, dump them instead 😀
Ok It’s always better for the starter to start first (pss is there a proverb or saying that fits well my claim here in English – I am a very active English learner ;-D)
Here you go:
$array=explode($string," ");
$con=$array[0];
$i=0;
foreach($array as $v)
{
if($i++ == 20){break;}
$con.=" ".$v[i];
}
Oh yezzz, Anyone can help with neater function or method please ?
array_slice()will slice the array for you. Just specify the beginning and end elements:If you want to get really compact (but a little less readable), do this instead:
$twentyWordswill be an array. If you want to concatenate it back into a string, just useimplode():