Is it possible to replace all the <br> tags with <p></p> with the PHP tools?

  • str_replace . Only probably <br> better to replace with </p><p> - cyadvert

1 answer 1

try this

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

Option number 2 (by Visman)

 function replace_br($data) { $data = preg_replace('#(?:<br\s*/?>\s*?)+#', '</p><p>', $data); return "<p>$data</p>"; } 
  • one
    Two or more <br> ( {2,} ) per pair </p><p> ? - Dmitriy Simushev
  • you can and so, just thought easier to remove than add - Saidolim
  • one
    Your regular expression does not match the single <br> tag. Proof: ideone.com/45rBAr - Dmitriy Simushev
  • @DmitriySimushev thanks a lot, fixed - Saidolim
  • 2
    {1,} is + . - Visman