0

I have a string that I want to shorten so that it is always 20 characters or less.

  • I don't want the words to be cut off.
  • When it is too long, I add (...) at the end and this (...) is included in the 20 characters.
$string = "Hello my name is Ana and I live in Egypt";
$length = 20;

if(strlen($string) > $length) {
  $string = wordwrap($string, $length - 6); // -6 because we count the chars of " (...)"
  $string = explode("\n", $string, 2);
  $string = $string[0].' (...)';
}

echo $string; // output "Hello my name (...)"

But now I want to do the exact same thing, but by keeping the last word.

So the expected ouput is : "Hello my (...) Egypt"

I really don't know what's the best approach to do this. If there are any useful functions I don't know about, or maybe Regex?

Vald
  • 237
  • 2
  • 8
  • what if the first and the last words have more than 20 caracters ? – Yassine CHABLI Apr 17 '20 at 22:33
  • convert the string into the array of words, find the length of the last word, find the length of " (...) ", remove the last word from the array of words, merge words into the string again and pass this string into `wordwrap` where max length is 20 - length of the last word - length of the " (...) " marker – user1597430 Apr 17 '20 at 22:34
  • to convert the string into an array of words, use explode() – C.B. Apr 17 '20 at 22:40
  • @MohammedYassineCHABLI There is no case where a word will be more than 20 characters long, and even if it is, then the string will be more than 20 characters long because I don't want a word to be cut off. – Vald Apr 17 '20 at 22:40
  • [Does this answer what you're asking?](https://stackoverflow.com/q/18612872/1415724) – Funk Forty Niner Apr 17 '20 at 22:43
  • 1
    I finally did it by following @user1597430 advice – Vald Apr 17 '20 at 22:52
  • You might find [this](https://stackoverflow.com/questions/49836558/split-string-at-space-after-certain-number-of-characters-in-javascript/49836804#49836804) helpful - it's written for Javascript but the regex will work fine for PHP. Just set the length to `14` instead of `154` (14 to allow for the `(...)`) – Nick Apr 17 '20 at 22:52
  • @Vald, my man. You may publish your own answer here and accept it. – user1597430 Apr 17 '20 at 22:54

1 Answers1

1

So here's how I did it. It doesn't handle cases where the first and last words are more than 20 characters long, but that's what I'm looking for because the priority is that the words are not cut.

function shorten($string, $length = 20) {
  if(strlen($string) > $length) {
    $words = explode(' ', $string);
    $lastWord = end($words);
    $charsLastWord = strlen($lastWord);

    $string = wordwrap($string, $length - 7 - $charsLastWord); // -7 because we count the chars of " (...) "
    $string = explode("\n", $string, 2);
    $string = $string[0].' (...) '.$lastWord;
  }
  return $string;
}

echo shorten("Hello my name is Ana and I live in Egypt");
// output Hello my (...) Egypt

echo shorten("Hello my name is Ana and I live in ACountryWithALongName");
// output Hello (...) ACountryWithALongName
Vald
  • 237
  • 2
  • 8