php - How to select first 10 words of a sentence? -


how i, output, select first 10 words?

implode(' ', array_slice(explode(' ', $sentence), 0, 10)); 

to add support other word breaks commas , dashes, preg_match gives quick way , doesn't require splitting string:

function get_words($sentence, $count = 10) {   preg_match("/(?:\w+(?:\w+|$)){0,$count}/", $sentence, $matches);   return $matches[0]; } 

as pebbl mentions, php doesn't handle utf-8 or unicode well, if concern can replace \w [^\s,\.;\?\!] , \w [\s,\.;\?\!].


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -