Search code examples
phpmysqlinternet-explorerhtml-listsnl2br

Nl2br on data except for <li> tags


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:

http://logblog.net84.net/

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>';
          }
    ...

Solution

  • 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?