从MySql数据库中提取文本并将其拆分为网站上的段落

从MySql数据库中提取文本并将其拆分为网站上的段落

问题描述:

Can I just insert formated text into database and retrieve it as that? Should formating ( like new line ) appear on website after pulled from database? I have tried it and it seems that is not case.

How to accomplish that?

Thanks

我可以将格式化文本插入数据库并检索它吗? 从数据库中提取后,是否应该在网站上显示格式化(如新行)? 我试过了,似乎不是这种情况。 p>

如何实现? p>

谢谢 p> div>

If you want it to be formatted as is inserted in the database you could output it to a <pre> tag to preserve formatting. If you just want to have the newline replaced by a html tag, you can do that easily:

<?php

//be on the safe side, as suggested by Christian Sciberras
$sanitized = htmlspecialchars($databaseContent, ENT_QUOTES);

echo str_replace(array("
", "
"), array("<br />", "<br />"), $sanitized);

Why?

I use str_replace() in this case to show you how it's done. I usually don't replace the new line with a break but with a </p><p> (and this is why I didn't go for a nl2br() solution) If you think you'd want paragraphs instead of line breaks, just change the replacement.

there is method in php that will convert all to <br> so you will have new lines in your html file

string nl2br ( string $string [, bool $is_xhtml = true ] )

$is_xhtml menans if your page is XHTML then new lines will be converted to <br/> otherwise just <br>

I hope this helps