Hello everyone.

There is a variable in a PHP script containing the text, say, $comment . The line breaks in the text are made by tags <br></br> , and these tags can be either one pair (opening - closing) or several (unlimited pairs).

You need to replace all these <br></br> with </p><p> in the $comment variable before $comment it.

  • Labels should not be код , теги and программирование , but programming languages. php for example - Mr. Black
  • If you are given an exhaustive answer, mark it as correct (a daw opposite the selected answer). - Nicolas Chabanovsky

3 answers 3

For example, the function str-replace :

 $comment = "<p>" . str_replace("<br /><br />", "</p><p>", $comment) . "</p>"; 

You can add a line to clear empty paragraphs:

 $comment = str_replace("<p></p>", "", $comment); 

    Something like this will probably suit you:

     <?php $comment = "TEXT <br>TEXT 1</br> TEXT <br> TEXT 2-1 <br> TEXT 2-2 </br> TEXT 2-3 </br> TEXT"; $count = 0; do { $comment = preg_replace("/<br>(.+?)<\/br>/", "</p>$1<p>", $comment, -1, $count); } while ($count > 0); echo($comment); ?> 

      $comment = str_replace('<br>', '<p>', $commnet);

      $comment = str_replace('<br/>', '<p/>', $commnet);

      then all tags will be replaced.