How to choose the first 10 words of a sentence?

How can I, from the output, select only the first 10 words?

+44
string substring php trim
May 10 '11 at 21:20
source share
13 answers
implode(' ', array_slice(explode(' ', $sentence), 0, 10)); 

To add support for other word breaks, such as commas and dashes, preg_match provides a quick way and does not require line breaks:

 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 very well, so if this is a concern, you can replace \w with [^\s,\.;\?\!] And \w with [\s,\.;\?\!] .

+110
May 10 '11 at 21:22
source share

Simple division into spaces will not work correctly if instead of space in the sentence structure there is an unexpected character or the sentence contains several combined spaces.

The next version will work no matter what โ€œspaceโ€ you use between words and can be easily expanded to handle other characters ... it currently supports any space character plus.;?

 function get_snippet( $str, $wordCount = 10 ) { return implode( '', array_slice( preg_split( '/([\s,\.;\?\!]+)/', $str, $wordCount*2+1, PREG_SPLIT_DELIM_CAPTURE ), 0, $wordCount*2-1 ) ); } 

Regular expressions are ideal for this problem, because you can easily make the code flexible or strict as you like. However, you need to be careful. I specifically approached the above targeting for spaces between words - not the words themselves; because itโ€™s quite difficult to unambiguously indicate what the word will define.

Take the word boundary \w or its inverse \w . I rarely rely on them, mainly because - depending on the software you use (for example, certain versions of PHP) - they do not always include UTF-8 or Unicode characters .

In regular expressions, it is better to be specific at all times. So that your expressions can handle things like the following, no matter where they appear:

 echo get_snippet('   ,   ', 5); /// outputs:    ,  

Avoiding separation may be useful, but in terms of performance. So you can use the Kelly approach, but switch to \w on [^\s,\.;\?\!]+ And \w on [\s,\.;\?\!]+ . Although I personally like the simplicity of the cleavage expression used above, it is easier to read and therefore change. However, the PHP function stack is a little ugly :)

+48
Sep 16
source share

http://snipplr.com/view/8480/a-php-function-to-return-the-first-n-words-from-a-string/

 function shorten_string($string, $wordsreturned) { $retval = $string; // Just in case of a problem $array = explode(" ", $string); /* Already short enough, return the whole thing*/ if (count($array)<=$wordsreturned) { $retval = $string; } /* Need to chop of some words*/ else { array_splice($array, $wordsreturned); $retval = implode(" ", $array)." ..."; } return $retval; } 
+6
May 10 '11 at 21:22
source share

I suggest using str_word_count :

 <?php $str = "Lorem ipsum dolor sit amet, consectetur adipiscing elit"; print_r(str_word_count($str, 1)); ?> 

The above example outputs:

 Array ( [0] => Lorem [1] => ipsum [2] => dolor [3] => sit [4] => amet [5] => consectetur [6] => adipiscing [7] => elit ) 

Use the loop to get the right words.

Source: http://php.net/str_word_count

+2
Dec 10 '14 at 10:12
source share

To select 10 words of a given text, you can implement the following function:

 function first_words($text, $count=10) { $words = explode(' ', $text); $result = ''; for ($i = 0; $i < $count && isset($words[$i]); $i++) { $result .= $words[$i]; } return $result; } 
+2
Feb 15 '17 at 18:40
source share

This is easy to do with str_word_count()

 $first10words = implode(' ', array_slice(str_word_count($sentence,1), 0, 10)); 
+2
May 10 '17 at 17:30
source share

This can help you. The return function N is not. words

 public function getNWordsFromString($text,$numberOfWords = 6) { if($text != null) { $textArray = explode(" ", $text); if(count($textArray) > $numberOfWords) { return implode(" ",array_slice($textArray, 0, $numberOfWords))."..."; } return $text; } return ""; } } 
+1
Mar 04 '14 at 10:20
source share

This is what we are looking for. Just cut the n inserted into your program and run.

 function shorten_string($string, $wordsreturned) /* Returns the first $wordsreturned out of $string. If string contains fewer words than $wordsreturned, the entire string is returned. */ { $retval = $string; // Just in case of a problem $array = explode(" ", $string); if (count($array)<=$wordsreturned) /* Already short enough, return the whole thing */ { $retval = $string; } else /* Need to chop of some words */ { array_splice($array, $wordsreturned); $retval = implode(" ", $array)." ..."; } return $retval; } 

and just call the function in your code block just like

 $data_itr = shorten_string($Itinerary,25); 
0
Dec 09 '14 at 20:52
source share

I do this:

 function trim_by_words($string, $word_count = 10) { $string = explode(' ', $string); if (empty($string) == false) { $string = array_chunk($string, $word_count); $string = $string[0]; } $string = implode(' ', $string); return $string; } 

Compatible with UTF8 ...

0
Mar 05 '15 at 11:54
source share

This can help you. The function returns 10 no. of words no. of words .

 function num_of_word($text,$numb) { $wordsArray = explode(" ", $text); $parts = array_chunk($wordsArray, $numb); $final = implode(" ", $parts[0]); if(isset($parts[1])) $final = $final." ..."; return $final; return; } echo num_of_word($text, 10); 
0
Nov 18 '15 at 5:11
source share

try it

 $str = 'Lorem ipsum dolor sit amet,consectetur adipiscing elit. Mauris ornare luctus diam sit amet mollis.'; $arr = explode(" ", str_replace(",", ", ", $str)); for ($index = 0; $index < 10; $index++) { echo $arr[$index]. " "; } 

I know that this is not the time to answer, but let new visitors choose their own answers.

0
Nov 19 '15 at 8:38
source share
  function get_first_num_of_words($string, $num_of_words) { $string = preg_replace('/\s+/', ' ', trim($string)); $words = explode(" ", $string); // an array // if number of words you want to get is greater than number of words in the string if ($num_of_words > count($words)) { // then use number of words in the string $num_of_words = count($words); } $new_string = ""; for ($i = 0; $i < $num_of_words; $i++) { $new_string .= $words[$i] . " "; } return trim($new_string); } 

Use it as follows:

 echo get_first_num_of_words("Lorem ipsum dolor sit amet consectetur adipisicing elit. Aliquid, illo?", 5); 

Exit: Lorem ipsum dolor sit amet

This feature also works great with Unicode characters such as Arabic characters.

 echo get_first_num_of_words("ู†ู…ูˆุฐุฌ ู„ู†ุต ุนุฑุจูŠ ุงู„ุบุฑุถ ู…ู†ู‡ ุชูˆุถูŠุญ ูƒูŠู ูŠู…ูƒู† ุงุณุชุฎู„ุงุต ุฃูˆู„ ุนุฏุฏ ู…ุนูŠู† ู…ู† ุงู„ูƒู„ู…ุงุช ุงู„ู…ูˆุฌูˆุฏุฉ ูู‰ ู†ุต ู…ุนูŠู†.", 100); 

Exit: ู†ู…ูˆุฐุฌ ู„ู†ุต ุนุฑุจูŠ ุงู„ุบุฑุถ ู…ู†ู‡ ุชูˆุถูŠุญ ูƒูŠู ูŠู…ูƒู† ุงุณุชุฎู„ุงุต ุฃูˆู„ ุนุฏุฏ ู…ุนูŠู† ู…ู† ุงู„ูƒู„ู…ุงุช ุงู„ู…ูˆุฌูˆุฏุฉ ูู‰ ู†ุต ู…ุนูŠู†.

0
Nov 23 '15 at 14:17
source share

I donโ€™t understand why all this mess when there is a built-in Wordpress function:

 <?= wp_trim_words(get_the_content(), 15, '...') ?> 

This is an echo of the first 15 words of content (it works inside a regular loop) and adds an ellipsis.

-four
Aug 03 '15 at 21:41
source share



All Articles