gpt4 book ai didi

PHP:向来自数据库的文本添加标签

转载 作者:行者123 更新时间:2023-11-30 22:26:47 25 4
gpt4 key购买 nike

我在 MySql 数据库中有一段文本。我想我不应该将标签放入数据库中。如果是这样,当我在 html 页面中回显该文本时如何添加 p 标签?

例如我在数据库中有(三段):

第 1 段

第 2 段

第 3 段

当我在页面中使用php回显文本时:

$text = $row['first'];
echo $text;//this gives: paragraph 1paragraph 2paragraph 3

这是我试图得到的:

<p>paragraph 1</p> 

<p>paragraph 2</p>

<p>paragraph 3</p>

最佳答案

如果你知道你有 \n换行符,然后:

    // the full text
$text = "paragraph 1\n\nparagraph 2\n\nparagraph 3";

// remove \n and space at start/end
$text = trim( $text );

// the paragraph break
$paragraphBreak = array("\r\n\r\n", "\n\r\n\r", "\n\n", "\r\r");

// add the <p> tag
$text = '<p>'.str_replace( $paragraphBreak, '</p><p>', $text ).'</p>';

// display with <br> tag
echo nl2br( $text );

这将添加 <br>标记,并创建一个新的 <p>如果你跳一行

关于PHP:向来自数据库的文本添加标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34975255/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com