new line to <p> in php
Asked Answered
C

7

6

I currently have a lot of jokes in a database that are formated with nl2br() which produces...

This is just dummy text. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
<br /><br />
Vestibulum gravida justo in arcu mattis lacinia. Mauris aliquet mi quis diam euismod blandit ultricies ac lacus.
<br /><br />
Aliquam erat volutpat. Donec sed nisi ac velit viverra hendrerit.
<br />
Praesent molestie augue ligula, quis accumsan libero.

I need a php function that rather convert <br /><br /> into a <p></p> and if there is only 1 <br /> then leave it alone. Also trim any whitespace

So the end result would be...

<p>This is just dummy text. Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<p>Vestibulum gravida justo in arcu mattis lacinia. Mauris aliquet mi quis diam euismod blandit ultricies ac lacus. </p>
Aliquam erat volutpat. Donec sed nisi ac velit viverra hendrerit.<br />
Praesent molestie augue ligula, quis accumsan libero.

can someone point me in the right direction or give me an simple function that does this? thanks

Coupe answered 15/2, 2013 at 23:55 Comment(1)
You want to trim each row?Delaryd
E
11

Use PHP's string replace function to replace all <br /><br /> with </p><p>. http://php.net/manual/en/function.str-replace.php

Sample Code:

$stringWithBRs = nl2br($originalString)
$stringWithPs = str_replace("<br /><br />", "</p>\n<p>", $stringWithBRs);
$stringWithPs = "<p>" . $stringWithPs . "</p>";

Or, you can use the following code without even calling the nl2br() function.

$stringWithPs = str_replace("\n\n", "</p>\n<p>", $originalString);
$stringWithPs = "<p>" . $stringWithPs . "</p>";
Exhilaration answered 16/2, 2013 at 0:0 Comment(2)
he wants to put string into <p> tags =)Fennelly
Yea, its the same thing. He can replace all the <br /><br /> with </p><p> and then wrap the whole text with a <p> and `<\p>Exhilaration
B
3

Simply direct convert the original string:

$text=str_replace("<br /><br />", "</p><p>", $original_string);

And in HTML, somewhere like this:

<p>
 <?php echo $text; ?>
</p>
Broadus answered 29/1, 2014 at 17:39 Comment(0)
H
3

A one-liner:

$sNewString = implode('</p><p>',explode('<br /><br />', nl2br($sOriginalString)))

Explanation:

  1. Convert line breaks to br's
  2. Split string into array by these br's
  3. Glue the array back to a string by sticking P tags between each array item
Highboy answered 23/2, 2016 at 12:15 Comment(0)
B
0
<?php
   function nl2p($string = '') {
        $array = explode("\n", $string);
        $content = '';
        foreach ($array as $str) {
            $content .= "<p>" . $str . "</p>";
        }
        return $content;
    }

echo nl2p($content);

Breast answered 7/2, 2023 at 1:51 Comment(0)
V
0

Based on the previous answers, my code which generates the correct html with the p tags enclosed:

$text = nl2br($text);
$text = trim(preg_replace('/\s\s+/', ' ', $text));
$text = str_replace("<br /> <br />", "<br /><br />", $text);      
$text = implode('</p><p>',explode('<br /><br />', $text));
$text = '<p>'.$text.'</p>';
Vogeley answered 10/3, 2023 at 8:37 Comment(0)
W
-1
$new = '';
foreach(explode("\n<br /><br />\n", $text) as $p) // split by double newlines
    $new.= "<p>$p</p>\n"; // and convert each part to a paragraph


// or if you want to keep it in a single line
$new = join(array_map(function($p){return "<p>$p</p>\n";}, explode("\n<br /><br />\n", $text)));
Washedout answered 26/7, 2014 at 9:19 Comment(0)
H
-1

Try it

$old_string = preg_replace("/[\r\n]/","</p><p>",$old_string);
$new_string = "<p>" . $old_string . "</p>";
echo $new_string;
Herbert answered 9/3, 2018 at 19:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.