2
$theExcerpt = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis'

$theExcerptAppend = (strlen($theExcerpt) > 156) ? '...' : '';
$theExcerpt = preg_replace('/\s+?(\S+)?$/', '', substr($theExcerpt, 0, 156));
$theExcerpt .= $theExcerptAppend;

As long as the input phrase length exceeds 156 characters, the script works fine. However, when the length is less than 156 (as it is here at 154), the last word in being dropped off, even if the string, including the word, is still less than 156.

Note: I don't want the string to terminate in the middle of a word, but if the inclusion of the word does not exceed the strlen value of 156, it should be included.

RegEdit
  • 2,134
  • 10
  • 37
  • 63
  • Try `'/^\s+?(\S+)?$/'` (note the added `^`). – Halcyon Mar 29 '13 at 14:17
  • To clarify, I don't want the script to terminate in the middle of a word. Sorry I didn't include that in the original question. – RegEdit Mar 29 '13 at 14:20
  • possible duplicate of [Shorten String in PHP (full words only)](http://stackoverflow.com/questions/972010/shorten-string-in-php-full-words-only). And we have dozens of other duplicates which you didn't look through prior reposting this. – mario Mar 29 '13 at 14:31
  • @mario. I looked through several. A bit presumptive to claim to know the actions of others, secret powers? – RegEdit Mar 29 '13 at 14:48

4 Answers4

5

Using substr and strrpos

if (strlen($theExcerpt) > 156) {
    $theExceprt = substr($theExcerpt, 0, 156);
    $theExcerpt = substr($theExcerpt, 0, strrpos($theExcerpt, ' '));        
    $theExcerpt .= '...';
}
Crisp
  • 11,417
  • 3
  • 38
  • 41
1

I think someone posted a link to a duplicate. The accepted solution was:

/^.{1,156}\b/

Now, this will ALWAYS be less than 156 chars. If the 156th char is in a middle of a word, it will cut the last word. Some change could be made to have the opposite effect though.

Note: simply apply preg_match to your string with this regex.

Edit:
Opposite effect (having more than 156 characters to get the last word):

/^.{1,155}(.)?(?(1).*?\b)/
Loamhoof
  • 8,293
  • 27
  • 30
  • The accepted answer here handles the exception created by the "duplicate". – RegEdit Mar 29 '13 at 14:50
  • Well, no point discussing it as you have had your answer. Nevertheless, I didn't have any exception running both of those regex. – Loamhoof Mar 29 '13 at 14:55
0

How about :

$theExcerpt = preg_replace('/(?=.{156})\s+?(\S+)?$/', '', substr($theExcerpt, 0, 156));

This will treat the sentence only if it is more than 156 char long.

Toto
  • 89,455
  • 62
  • 89
  • 125
0

Try the following:

$string = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus egestas, lacus non dapibus placerat, justo mi adipiscing libero, id ultrices neque metus nec lorem. Quisque vitae dui facilisis ligula tristique dapibus. Ut egestas ligula in tortor facilisis pharetra id vitae eros. Donec commodo laoreet nisi porttitor tincidunt. Donec tortor enim, pharetra in accumsan sit amet, scelerisque ac massa. Morbi massa erat, mattis non faucibus a, feugiat imperdiet lectus. Praesent tincidunt libero id enim cursus non sagittis nisl accumsan. Maecenas massa lorem, consectetur ut rhoncus ac, ullamcorper a tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque sit amet eros.';

$limit = 156;
$output = preg_replace('/^(.{'.$limit.'})(\S|\s|\w+)(.*)/', '$1$2 ...', $string);

echo $output;
HamZa
  • 14,671
  • 11
  • 54
  • 75