显示带空格或中断的获取数据 - PHP

时间:2016-07-15 11:25:13

标签: php html css

我想用一些空格或断行显示数据。有人可以帮我这个吗?我正在使用textarea添加和编辑新闻内容。我的新闻内容的数据类型为text

我的内容想要像这样显示。 enter image description here

这是我的工作。如你所见,那里没有休息 enter image description here

这是获取数据的代码。

<?php
    include_once('connection.php');
     $newsid = $_GET['newsid'];

     $sql ="SELECT * FROM news WHERE news_id = '$newsid'";
     $result = mysqli_query($con, $sql);

     while($row = mysqli_fetch_array($result)){
     $title = $row['news_title'];
     $date = $row['news_date'];
     $content = $row['news_content'];
     $newsimage = $row['news_image'];
         }         
    ?>

 <img src="<?php echo $newsimage; ?>" alt="court" style="width:100%; height:430px; ">
    <div class="content">
        <div class="container">
            <div class="row">
               <div class="fix midbar">
                  <div class="viewnews">
                        <h3><?php echo $title; ?> </h3>
                         <p>Date posted: <?php echo $date; ?></p>
                         <p><?php echo $content; ?></p>
                  </div>
               </div>

            </div>
        </div>
    </div>

3 个答案:

答案 0 :(得分:1)

您可以使用 nl2br

nl2br - 在字符串中的所有换行符之前插入HTML换行符

<强>描述

string nl2br ( string $string [, bool $is_xhtml = true ] )

返回在所有换行<br /> or <br>

之前插入(\r\n, \n\r, \n and \r).的字符串

答案 1 :(得分:1)

在换行符上打破内容。您可以使用PHP的nl2br函数:

while($row = mysqli_fetch_array($result)){
    $title = $row['news_title'];
    $date = $row['news_date'];
    $content = nl2br($row['news_content']);
    $newsimage = $row['news_image'];
}

nl2br将使用HTML换行符(<br>)代码替换所有换行符。

但是,这只是一个部分解决方案,因为它只会添加一个换行符,如下所示:

  

将一个文本文本一个文本一个   发短信两个文本文本

显然,这并不是你想要的。你想要这个:

  

将一个文本文本一个文本

     

发送两个文本文本

要对此进行更多控制,请在换行符上拆分$content,并将每个部分换成span标记。

while($row = mysqli_fetch_array($result)){

    $title = $row['news_title'];
    $date = $row['news_date'];
    $newsimage = $row['news_image'];
    $content = $row['news_content'];

    // splits the content on line breaks
    $content_array = explode("\r\n", $content);

    // go through each item and wrap them into a span 
    // (you can style it later with css, if you wish)
    foreach ($content_array as &$item) {
        $item = '<span class="news_item">'.$item.'</span>';
    }

    // Join the elements of the array back into one string, 
    // and use a double line break as the glue
    $content = implode("<br /><br />", $content_array);
}

答案 2 :(得分:1)

  

您需要将\ n替换为break row tag

试试这个

echo nl2br($content);