我最近在弄一个论坛,遇到从数据库输出到页面的文章内容该换行的地方不换行。数据库里的数据确定没问题,是换行显示的。还请高手指点.(ps:已经用网上传的什么nl2br函数试过,不起作用)php数据换行

解决方案 »

  1.   

    你要把文本中的换行\r\n  换成html标签 <br />
      

  2.   

    哦,nl2br 就是做这个事的。
    那你就先看下数据内容,另外还要看你是否直接呈现在页面上还是容器上
      

  3.   

    查一下页面的css样式定义是否有问题
      

  4.   

    <?php # Script 17.5 - read.php
    // This page shows the messages in a thread.
    include ('includes/header.html');// Check for a thread ID...
    $tid = FALSE;
    if (isset($_GET['tid']) && filter_var($_GET['tid'], FILTER_VALIDATE_INT, array('min_range' => 1)) ) {

    // Create a shorthand version of the thread ID:
    $tid = $_GET['tid'];

    // Convert the date if the user is logged in:
    if (isset($_SESSION['user_tz'])) {
    $posted = "CONVERT_TZ(p.posted_on, 'UTC', '{$_SESSION['user_tz']}')";
    } else {
    $posted = 'p.posted_on';
    } // Run the query:
    $q = "SELECT t.subject, p.message, first_name, DATE_FORMAT($posted, '%e-%b-%y %l:%i %p') AS posted FROM threads AS t LEFT JOIN posts AS p USING (thread_id) INNER JOIN users AS u ON p.user_id = u.user_id WHERE t.thread_id = $tid ORDER BY p.posted_on ASC";
    $r = mysqli_query($dbc, $q);
    if (!(mysqli_num_rows($r) > 0)) {
    $tid = FALSE; // Invalid thread ID!
    }

    } // End of isset($_GET['tid']) IF.if ($tid) { // Get the messages in this thread...

    $printed = FALSE; // Flag variable. // Fetch each:
    while ($messages = mysqli_fetch_array($r, MYSQLI_ASSOC)) { // Only need to print the subject once!
    if (!$printed) {
    echo "<h2>{$messages['subject']}</h2>\n";
    $printed = TRUE;
    }

    // Print the message:

    $messages['message'] = preg_replace('\r\n','<br/>',$messages['message']);
    echo "<p>{$messages['first_name']} ({$messages['posted']})<br/>{$messages['message']}</p><br/>\n"; } // End of WHILE loop.

    // Show the form to post a message:
    include ('includes/post_form.php');

    } else { // Invalid thread ID!
    echo '<p>This page has been accessed in error.</p>';
    }include ('includes/footer.html');
    ?>
      

  5.   

    数据的换行显示和css有关系?????