开发者

Pull text from MySql database and split it in paragraph on website

Can I just insert formated text into database and retrieve it as that? Should formating ( like new line ) appear on website after pulled from dat开发者_运维技巧abase? I have tried it and it seems that is not case.

How to accomplish that?

Thanks


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("\r\n", "\n"), 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 \n 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜