开发者

php echo doesnt print newlines

开发者 https://www.devze.com 2022-12-24 09:08 出处:网络
I started doing a new project in PHP / MySql . The Aim of this project is to manage articles for a magazine that i am the editor of.

I started doing a new project in PHP / MySql . The Aim of this project is to manage articles for a magazine that i am the editor of.

So the content of the articles, i decided i would store with the "TEXT" column type of MySql

Now w开发者_如何学Chen i retrieve this column and print it with echo, the newlines are not there. Its all on the same line.

$resset = mysql_query("select txt from articles where id = 1");
$row = mysql_fetch_assoc($resset);
$txt = $row['txt'];
echo $txt; //doesnt print it as it is in the database as it was typed (multiline)

Find below, the text as it looks in the database and as it looks when it is echoed

in the databse, it is with new lines http://img413.imageshack.us/img413/4195/localhostlocalhostzxcvb.jpg

Text as it looks when echod http://img718.imageshack.us/img718/1700/sdashboardmozillafirefo.jpg

But within the database, its stored with newlines.

Has anybody else encountered this problem?

Please help me as my project depends on this :|


Whitespace in HTML is folded into a single space. Use nl2br() if you want to maintain newlines in HTML.


Have you tried

echo $txt."<br/>";


alternatively, you can put your output between <pre> tags:

echo "<pre>";
$resset = mysql_query("select txt from articles where id = 1");
$row = mysql_fetch_assoc($resset);
$txt = $row['txt'];
echo $txt; //doesnt print it as it is in the database as it was typed (multiline)
echo "</pre>";

btw,

echo $txt."<br/>";

may not work since it will just append newline at the end but not within $txt string


I know this isn't part of your question, but if you additionally want new lines in your HTML code but not your presentation, use double quotes with \n. This can help keep the HTML really tidy.

echo "\n";
0

精彩评论

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

关注公众号