除
我希望能够从 mysql 获取 html 格式的数据,并将新行转换为
或
标签。
我已经能够通过 CSS 中的 white-space: pre-wrap
有效地做到这一点,除了 IE7 及以下版本。
我的问题是,如果我在浏览器为 <=IE7 的情况下在数据上放置条件 nl2br() ,那么它会在
标记上创建换行符,该标记已经有换行符,在子弹之间创造额外的空间。有更好的方法吗?
我的测试页面是:
我的代码是(此时在时间):
...
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<style>
p { white-space:pre-wrap;white-space:-moz-pre-wrap;word-wrap:break-word }
</style>
<body>
<?php
...
$conn = mysql_connect($host, $user, $pw) or die('Error connecting to mysql');
mysql_select_db($db);
mysql_set_charset('utf8', $conn);
$result = mysql_query("SELECT * FROM wp_posts WHERE post_status='publish' AND post_type='post'");
while($row = mysql_fetch_array($result))
{
echo '<h2>'.$row['post_title'].'</h2>';
echo '<p>'.$row['post_content'].'</p>';
}
...
I would like to be able to get html-formatted data from mysql, and turn the new lines into <br>
or <p>
tags.
I have been able to do this effectively with white-space: pre-wrap
in the CSS, except for with IE7 and under.
My issue is that if I put a conditional nl2br() on the data if the browser is <=IE7, then it would create line breaks on the <li>
tags, which already have line breaks, creating extra space between the bullets.
Is there a better way to do this?
My test page is:
And my code is (at this point in time):
...
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<style>
p { white-space:pre-wrap;white-space:-moz-pre-wrap;word-wrap:break-word }
</style>
<body>
<?php
...
$conn = mysql_connect($host, $user, $pw) or die('Error connecting to mysql');
mysql_select_db($db);
mysql_set_charset('utf8', $conn);
$result = mysql_query("SELECT * FROM wp_posts WHERE post_status='publish' AND post_type='post'");
while($row = mysql_fetch_array($result))
{
echo '<h2>'.$row['post_title'].'</h2>';
echo '<p>'.$row['post_content'].'</p>';
}
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否想绕过 WordPress 的自动段落替换?如果是这样,有一个内置函数适合您 - http://codex.wordpress.org/Function_Reference/ wpaautop
如果不是这个,那么你能更具体一点吗 - 看起来你的测试页面链接没有出现在帖子上?
Are you trying to get around WordPress's automatic Paragraph replacement? If so there is a built-in a function for you - http://codex.wordpress.org/Function_Reference/wpautop
If not this then can you be a little more specific - it looks like your test page link did not come through on the post?