How to use the regular expression to display text from the database and, at the same time, wherever there are more than 2 line breaks in a row, remove the rest after 2x?
Here's how here - I press Enter 10 times in a row, anyway, the text will be displayed like this:

Only 1 blank line.

  • So the answer for PHP is accepted? Then it is still simpler - preg_replace('~(\R)+~', '$1', $input); , well, or preg_replace('~\R{2,}~', '<br />', $input); . - Wiktor Stribiżew
  • By “hyphenation” do you mean moving to a new line? Then it may be better to formulate this way "there should be no more than one empty line in a row." Still wondering what is used as a line delimiter? "\ n", "\ r \ n" or "<br>"? - sercxjo

1 answer 1

For text (not for HTML), you can use a replacement (php option)

 $str = preg_replace('%((?:\r\n|\r|\n){2})[\r\n]+%', '$1', $str); 

Example here https://regex101.com/r/sR2iT9/1

PS In the regular schedule there are three options for ending lines: \r\n , \r and \n