将带有换行符的断行字符串分解为带有PHP的HTML段落 [英] Break string with line breaks into HTML paragraphs with PHP

查看:84
本文介绍了将带有换行符的断行字符串分解为带有PHP的HTML段落的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从我的MySQL数据库中获取文本,并将其分解成多行(段落)。它存储在变量 $ post_data ['content'] 中。我如何使用换行符打印出来?



我目前使用的代码:

  $ post_data ['content'] = explode(\\\
,$ post_data ['content']);
$ post =< p> 。 implode(< / p>< p>,array_values($ post_data ['content']))。 < / P> 中;

这并不适用于我想要的程度。



如果我在数据库中有这样的文本:

 第1行第1段,
第2行第1段。

第3行第2段
第4行第2段
第5行第2段

代码会显示像这样(我不需要):

 < p>第1行第1段,< / p> 

< p>第2行第1段。< / p>

< p>第3行第2段,< / p>

< p>第4行第2段,< / p>

< p>第5行第2段。< / p>

如果行之间没有空格(只有一个换行符),我希望它将段落组合在一起)。或许与爆炸后的数组有什么关系?

首先,用替换换行符< br / >

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

然后替换双重< br /> 使用闭合和开始的段落标记(原始换行符由 nl2br 维护,因此我使用正则表达式,它匹配所有换行符的样式):

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

请注意,这是XHTML语法,如果您想拥有HTML,请如下更改代码:

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

测试:

  $ post_data ['content'] =  第1行第1行
第2行第1行

第3行第2段
第4行第2段
第5行第2段
TXT;

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

echo $ post;

测试输出:

 < p>第1行第1段,< br> 
第2行第1段。< / p>

< p>第3行第2段,< br>
第4行第2段,< br>
第5行第2段。< / p>


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?

解决方案

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>

这篇关于将带有换行符的断行字符串分解为带有PHP的HTML段落的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆