6

Possible Duplicate:
How to select first 10 words of a sentence?

I want to show 10 words of the content words not characters

$string = 'Lorem ipsum dolor sit amet,consectetur adipiscing elit. Mauris ornare luctus diam sit amet mollis.';

should the result to be
"Lorem ipsum dolor sit amet,consectetur adipiscing elit. Mauris ornare"

Community
  • 1
  • 1
Déjà Bond
  • 291
  • 4
  • 8
  • 32
  • 3
    depending on the use-case, you may prefer to use CSS `text-overflow:ellipsis;` for this rather than truncating your string. – Spudley Sep 16 '12 at 07:28

5 Answers5

18

Try this function:

function shorten_string($string, $wordsreturned)
{
  $retval = $string;
  $string = preg_replace('/(?<=\S,)(?=\S)/', ' ', $string);
  $string = str_replace("\n", " ", $string);
  $array = explode(" ", $string);
  if (count($array)<=$wordsreturned)
  {
    $retval = $string;
  }
  else
  {
    array_splice($array, $wordsreturned);
    $retval = implode(" ", $array)." ...";
  }
  return $retval;
}

On your text, so like this:

$string = 'Lorem ipsum dolor sit amet,consectetur adipiscing elit. Mauris ornare luctus diam sit amet mollis.';
$firsttenwords = shorten_string($string, 10);

From here.

UPDATE: Now it's space-compliant, and also new-line compliant.

RichardB
  • 2,615
  • 1
  • 11
  • 14
Lucas
  • 16,930
  • 31
  • 110
  • 182
  • 2
    @thi - There is no need to poke the OP until he accepts/votes on your answer. As you can see from his profile the user knows very well how to accept an answer. By no means is anyone *required* to click accept. As for votes, you simply copied some code you found elsewhere - not vote worthy IMO. – Lix Sep 16 '12 at 07:22
  • @think123 I was accept your answer but was a massage tell me i have to wait 3 minutes to accept any answers – Déjà Bond Sep 16 '12 at 07:27
  • Just as a warning to those who use this code the above will only work if your sentences are formed using exact grammar. i.e. `"what happens if you have a sentence-----like this."` *(imaging the dashes are all spaces)*, or `"this sentence\nhas a new line in it"` -- a perfect example of this exists in the OP's request *(a comma with no space)*. – Pebbl Sep 16 '12 at 07:38
  • @pebbl Then it wouldn't exactly be correct grammar, would it? Anyway, about the spacing, I think I can fix that one. – Lucas Sep 16 '12 at 08:42
  • @think123 precisely... and where did anyone say anything about the sentence having correct grammar? If these sentences are being pulled from user input, wysiwyg, xml or - even worse - from other web pages; then each of those occurances is not only possible, but quite likely. I'd agree with you if the OP had stated they were pulling everything from "the little book of amazing grammar and how to write good" though. – Pebbl Sep 16 '12 at 08:53
  • @pebbl Could you suggest a method for the `------` case? – Lucas Sep 16 '12 at 08:55
  • @pebbl I fixed the other two... – Lucas Sep 16 '12 at 08:56
  • @think123 - Rather mad combination of lookahead look behind regexp? But hey it works... :) my solution for problems mentioned would be `$str = preg_replace('/\s+|(,)+[^ ]/','$1 ',$str);` - the first part handles multiple whitespace being converted to a singular space (includes \n) and the second handles commas not followed by spaces. For the record though this is being rather specific and transformative to the original string. However it will solve the most likely problems. – Pebbl Sep 16 '12 at 09:28
4

This version will work no matter what kind of "space" you use between words and can be easily extended to handle other characters... it currently supports any white space character plus , . ; ? !

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

For those who should might prefer the original formatting :)

function getSnippet( $str, $wordCount = 10 ) {
  return implode( '', array_slice( preg_split('/([\s,\.;\?\!]+)/', $str, $wordCount*2+1, PREG_SPLIT_DELIM_CAPTURE), 0, $wordCount*2-1 ) );
}
Pebbl
  • 34,937
  • 6
  • 62
  • 64
  • 2
    -1 for bad indentation but +2 for a nice solution = total of +1 :P – Nir Alfasi Sep 16 '12 at 08:06
  • 1
    Ah heh cheers :) I only did that because I have a vendetta against the appearance of mid-page horizontal scroll bars - I wish SO would implement an on hover code display or something... – Pebbl Sep 16 '12 at 08:08
3

Try:

$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]. " ";
}

Output:
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris ornare

Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • 2
    Note there is no space in `amet,consectetur` – xdazz Sep 16 '12 at 06:52
  • @xdazz that should be a mistake, a proper sentence should have a space after a comma. Anyways, I changed my solution to accommodate this requirement as well. – Nir Alfasi Sep 16 '12 at 07:16
  • @alfasin In the world of sentence structure people always make mistakes... and in the world of typing web content people are even worse. imo your code should be able to handle more than just a singular problematic use-case. – Pebbl Sep 16 '12 at 07:46
  • 2
    @pebbl try to eliminate it and you'll always run into new cases that you didn't consider. That's totally fine - the only thing that matters is how critical is it to you. If you need perfection, then yes, by all means, handle all the possibilities you can think of, and continue to iterate over new cases until you've reached a satisfactory quality of output. But if you just want to grab the first 10 words and put "read more..." link afterwards, then TMO it will be an overkill. But that's just my two cents ;) – Nir Alfasi Sep 16 '12 at 08:01
  • @alfasin good points *(there is a limit to how much "perfection" you try and attain in any situation - if there isn't you tend to go a bit bonkers ;)*, I'm still baffled that there isn't one RegExp solution proffered though, even your own would be improved by using the preg versions of explode and replace - and would wipe out a number of foreseeable issues. I guess it's down to the fact that ppl don't think trimming paragraphs arbitrarily is a good idea - so therefore it doesn't matter if you end up with 12 words or 6 *(which is probably true)*. – Pebbl Sep 16 '12 at 08:20
2

We can retrieve the words in a string using str_word_count function .

For more description about the function please refer the below link

http://php.net/manual/en/function.str-word-count.php

For displaying only 10 words in the string please refer the below code snippet

    $str='Lorem ipsum dolor sit amet,consectetur adipiscing elit. Mauris ornare luctus diam sit amet mollis.';
    $words=str_word_count($str,true);
    $a=array_slice($words,10);
    $s=join('',$a);
    echo $s;
Rubin Porwal
  • 3,736
  • 1
  • 23
  • 26
  • This will not work in order to work you have to explode $words to create an array because array_slice requires arrays. Also str_word_count just counts the word numbers used like you do, the right way is str_word_count($str,1)! – Florin Andrei Apr 01 '17 at 08:47
  • This will work : $str='Lorem ipsum dolor sit amet,consectetur adipiscing elit. Mauris ornare luctus diam sit amet mollis.'; $words = str_word_count($str,1); $words = array_slice($words,0,10); $words = implode(" ", $words); echo $words; – Florin Andrei Apr 01 '17 at 08:59
-1

Try like this

$str = 'Lorem ipsum dolor sit amet,consectetur adipiscing elit. Mauris ornare luctus diam sit amet mollis.';
$arr = explode(" ", str_replace(",",", ",$str), 10);
echo implode(" ", $arr);
Justin John
  • 9,223
  • 14
  • 70
  • 129
  • http://php.net/manual/en/function.explode.php If limit is set and positive, the returned array will contain a maximum of limit elements with the last element containing the rest of string. – Stelian Nov 03 '14 at 21:59