php nl2p,PHP函数nl2br()与nl2p()函数

这段代码定义了一个名为`nl2p`的PHP函数,该函数将字符串中的换行符转换为HTML段落或换行标签。它接受三个参数:原始字符串、是否保留单行换行和是否使用XML格式。通过正则表达式,它能够将多行换行转换成`<p>`标签,同时根据设置决定是否将单行换行转换为`<br>`标签。这个函数对于格式化包含多行文本的数据到HTML中非常有用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[php]代码库/**

* Returns string with newline formatting converted into HTML paragraphs.

*

* @param string $string String to be formatted.

* @param boolean $line_breaks When true, single-line line-breaks will be converted to HTML break tags.

* @param boolean $xml When true, an XML self-closing tag will be applied to break tags (
).

* @return string

*/

function nl2p($string, $line_breaks = true, $xml = true)

{

// Remove existing HTML formatting to avoid double-wrapping things

$string = str_replace(array('

', '

', '
', '
'), '', $string);

// It is conceivable that people might still want single line-breaks

// without breaking into a new paragraph.

if ($line_breaks == true)

return '

'.preg_replace(array("/([\n]{2,})/i", "/([^>])\n([^\n

", '
'), trim($string)).'

';

else

return '

'.preg_replace("/([\n]{1,})/i", "

\n

", trim($string)).'

';

}

694748ed64b9390909c0d88230893790.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值