使用空格或break-PHP显示获取数据
我想用一些空格或换行符来显示数据。有人能帮我吗?Im在添加和编辑新闻内容时使用使用空格或break-PHP显示获取数据,php,html,css,Php,Html,Css,我想用一些空格或换行符来显示数据。有人能帮我吗?Im在添加和编辑新闻内容时使用textarea。我的新闻内容的数据类型是text 我希望内容显示为这样。 这是我的作品。正如你所看到的,那里没有休息 下面是获取数据的代码 <?php include_once('connection.php'); $newsid = $_GET['newsid']; $sql ="SELECT * FROM news WHERE news_id = '$newsid'";
textarea
。我的新闻内容的数据类型是text
我希望内容显示为这样。
这是我的作品。正如你所看到的,那里没有休息
下面是获取数据的代码
<?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>
“alt=”court“style=”宽度:100%;高度:430px;">
公布日期:
您可以使用nl2br
nl2br-在字符串中的所有换行符之前插入HTML换行符
说明
string nl2br ( string $string [, bool $is_xhtml = true ] )
返回在所有换行符前插入
或
的字符串(\r\n\n\r\n和\r)。
您可以使用nl2br
nl2br-在字符串中的所有换行符之前插入HTML换行符
说明
string nl2br ( string $string [, bool $is_xhtml = true ] )
返回在所有换行符之前插入
或
(\r\n\n\r\n和\r)的字符串。
在换行符上断开内容。可以使用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换行符(
)标记替换所有换行符
但是,这只是一个部分解决方案,因为它只会添加一个换行符,如下所示:
文本一文本一文本一文本一
文本两个文本文本
显然,这并不是你想要的。你想要的是:
文本一文本一文本一文本一
文本两个文本文本
要对此进行更多控制,请在换行符上拆分$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);
}
while($row=mysqli\u fetch\u数组($result)){
$title=$row['news_title'];
$date=$row['news_date'];
$newsimage=$row['news_image'];
$content=$row['news_content'];
//拆分换行符上的内容
$content\u数组=分解(“\r\n”,$content);
//检查每个项目并将其包装成一个跨度
//(如果愿意,您可以稍后使用css设置样式)
foreach($content\u数组作为&$item){
$item='.$item'.';
}
//将数组的元素重新连接为一个字符串,
//然后用一个双线断线作为胶水
$content=内爆(“
,$content\u数组);
}
在换行符上打断内容。您可以使用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换行符(
)标记替换所有换行符
但是,这只是一个部分解决方案,因为它只会添加一个换行符,如下所示:
文本一文本一文本一文本一
文本两个文本文本
显然,这并不是你想要的。你想要的是:
文本一文本一文本一文本一
文本两个文本文本
要对此进行更多控制,请在换行符上拆分$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);
}
while($row=mysqli\u fetch\u数组($result)){
$title=$row['news_title'];
$date=$row['news_date'];
$newsimage=$row['news_image'];
$content=$row['news_content'];
//拆分换行符上的内容
$content\u数组=分解(“\r\n”,$content);
//检查每个项目并将其包装成一个跨度
//(如果愿意,您可以稍后使用css设置样式)
foreach($content\u数组作为&$item){
$item='.$item'.';
}
//将数组的元素重新连接为一个字符串,
//然后用一个双线断线作为胶水
$content=内爆(“
,$content\u数组);
}
您需要用中断行标记替换\n
试试这个
echo nl2br($content);
您需要用中断行标记替换\n
试试这个
echo nl2br($content);
如果我添加时没有
怎么办?只是简单的剪切或空格。我猜新行字符会在那里。试试看。你不需要添加。@nethken:好吧,你需要把句子分开,最好的杠杆是换行符。如果文本没有换行符,就这样。它会变成一堵长长的文本墙。但是当如果你从Word或任何其他有段落的应用程序中粘贴,它们都会用换行符粘贴。如果我添加时没有
,怎么办?只需简单的剪切或空格。我猜新的换行符就会出现。试试看。你不需要添加。@nethken:好吧,你需要把句子分开,最好的办法就是换行符。如果文本没有换行符,就是这样。它会变成一堵长长的文本墙。但是当你从Word或任何其他有段落的应用程序中粘贴时,它们会用换行符粘贴。