Suppose there is such a variable:

$text = "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 nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat."; 

How to make a line break automatically so that the line has no more than 50 characters?

  • we run characters by string, remember the characters passed in the variable, and at the same time we count them. As you can see the space, if even <50 we type the memorized characters, we start to save them from scratch. If> 50 then print the line feed and after it the current word, put the character counter for the length of the printed word - Mike
  • one
    there is a duplicate of the topic, everything works there, and for Cyrillic too, ru.stackoverflow.com/questions/469212/… - Yuri Negometyanov
  • one
    @Mike is already done) - Yuri Negometyanov

2 answers 2

There is a wordwrap function

 string wordwrap ( string $str [, int $width = 75 [, string $break = "\n" [, bool $cut = false ]]] ) 

that is for soap so

 $text = wordwrap($text, 50, "\n", true); 
  • This function translates a string strictly on 50 characters. It is necessary so that if the 50th character is the middle of a word, then we transfer the string to the space before this word. - Frontender
  • Have you tried? It says what the words say. Wordwrap is the same. - VIP
  • Still, it works, but somehow strange. My line wrapping happens as if the cut parameter is divisible by 2. Instead of 50 - 25, instead of 40 - 20, etc. - Frontender
  • This happens with the Russian text. - Frontender
  • @ViktorPavlov utw8 wordwrap does not understand. So just about as I suggested, for example, a ready-made version on SO at stackoverflow.com/questions/3825226/… - Mike

Try

 $mess = mb_substr($item['mess'], 0, 50, 'UTF-8') . '...'; 

You can also try through html - enclose the text in a div and set a fixed width / height for it.

  • The variable is not displayed on the page, sent to the e-mail. - Frontender
  • The first option is not suitable? - Kuznetsov Maxim
  • And the first option just cuts the string, but does not transfer. - Frontender