Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Break string with line breaks into HTML paragraphs with PHP

I'm getting text from my MySQL database which is broken into lines (paragraphs). It's stored in the variable $post_data['content']. How do I make it print out with the line breaks?

The code I'm currently using:

$post_data['content'] = explode("\n", $post_data['content']);
$post = "<p>" . implode("</p><p>", array_values($post_data['content'])) . "</p>";

This doesn't work to the extent I want it to.

If I have text like this in the database:

line 1 paragraph 1,
line 2 paragraph 1.

line 3 paragraph 2,
line 4 paragraph 2,
line 5 paragraph 2.

The code would display like this (which I don't want):

<p>line 1 paragraph 1,</p>

<p>line 2 paragraph 1.</p>

<p>line 3 paragraph 2,</p>

<p>line 4 paragraph 2,</p>

<p>line 5 paragraph 2.</p>

I want it to group the paragraphs together if there's no white-space between lines (just one line break). Perhaps something with the array after the explode?

like image 680
Benny Avatar asked Feb 21 '13 22:02

Benny


1 Answers

First, replace line breaks with <br />:

$post = nl2br($post_data['content']);

Then replace double <br /> with closing and opening paragraph tag (the original line break is maintained by nl2br, so I use a regular expression, that matches all styles of line breaks):

$post = '<p>' . preg_replace('#(<br />[\r\n]+){2}#', '</p><p>', $post) . '</p>';

Note that this is XHTML syntax, if you want to have HTML, change the code as follows:

$post = nl2br($post_data['content'], false);
$post = '<p>' . preg_replace('#(<br>[\r\n]+){2}#', '</p><p>', $post) . '</p>';

Test:

$post_data['content'] = <<<TXT
line 1 paragraph 1,
line 2 paragraph 1.

line 3 paragraph 2,
line 4 paragraph 2,
line 5 paragraph 2.
TXT;

$post = nl2br($post_data['content'], false);
$post = '<p>' . preg_replace('#(<br>[\r\n]+){2}#', "</p>\n\n<p>", $post) . '</p>';

echo $post;

Test Output:

<p>line 1 paragraph 1,<br>
line 2 paragraph 1.</p>

<p>line 3 paragraph 2,<br>
line 4 paragraph 2,<br>
line 5 paragraph 2.</p>
like image 142
Fabian Schmengler Avatar answered Sep 29 '22 23:09

Fabian Schmengler