Search code examples
phpstringsubstringtrimtruncate

Get first N words of a string


How do I only get the first 10 words from a string?


Solution

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

    To add support for other word breaks like commas and dashes, preg_match gives a quick way and doesn't require splitting the 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 all that well, so if that is a concern then you can replace \w for [^\s,\.;\?\!] and \W for [\s,\.;\?\!].