0

Is it possible to make HTML Purifier to replace double <br> with <p> and at the same time not to delete single one? How easy it is to make?

We are trying to change double <br> into <p> in user genereated content and we use HTML Purifier for this purpose. But it seems this library cuts out all single <br> at the same time.

p.s. I am not a programmer, I am a product manager. And I asked to configure the text parser (we use HTML Purifier) on our service to let it use tags <p> insted of <br><br> in user generated texts. The programmer has done this but the next problem arises: the parser begins to cut out single line break <br>. And I was told that it is impossible or very hard to fix it. I am confused, I worked with regular expressions before and this was not a problem at all.

karaboz
  • 1
  • 4
  • 1
    Can you please update your question to show us what you have done / tried so far? – Ole Haugset Oct 07 '14 at 09:50
  • You can do this with a regular expression match/replace. No need for html purifier to do this. – ʰᵈˑ Oct 07 '14 at 09:51
  • @OleHaugset I am not a programmer, I am a product manager. And I asked to configure the text parser (we use HTML Purifier) on our service to let it use tags

    insted of

    in user generated texts. The programmer has done this but the next problem arises: the parser begins to cut out single line break
    . And I was told that it is impossible or very hard to fix it. And I am confused. I worked with regular expressions before and this was not a problem at all.

    – karaboz Oct 07 '14 at 14:01

1 Answers1

-2

It can be done pretty easy. The first (and definitely most easy method) is just to replace <br><br> with </p><p> and wrap the whole content in <p></p> tags.

$string = str_replace( '<br><br>', '</p><p>', $string );
$string = str_replace( '<br /><br />', '</p><p>', $string );
$string = str_replace( '<br><br />', '</p><p>', $string );
$string = str_replace( '<br /><br>', '</p><p>', $string );
echo "<p>$string</p>";

But a more cleaner solution is this. Use regex to identify the tags and then do a replace. It searches for <br>,<br >,<br />, etc. and then does a replace. Like so:

function replace_br($data) {
    $data = preg_replace('#(?:<br\s*/?>\s*?){2,}#', '</p><p>', $data);
    return "<p>$data</p>";
}

Info found here: php: replacing double <br /> with </p><p>

Community
  • 1
  • 1
tonsmets
  • 92
  • 3