Php 如何显示一个大段落的前几行?
请看图片: 这是我的社区论坛页面。我在第一行使用标题、回复和发布日期。现在我想添加另一个名为Description的专栏,它将显示整个主题的前几行。例如,这里的第二个标题是计算机的组件,但我想把它显示为……的组件。。。。。。。。你明白了吗 无论如何,我将所有数据存储在数据库中。要获得更多帮助,请参阅本页的代码Php 如何显示一个大段落的前几行?,php,mysql,while-loop,echo,Php,Mysql,While Loop,Echo,请看图片: 这是我的社区论坛页面。我在第一行使用标题、回复和发布日期。现在我想添加另一个名为Description的专栏,它将显示整个主题的前几行。例如,这里的第二个标题是计算机的组件,但我想把它显示为……的组件。。。。。。。。你明白了吗 无论如何,我将所有数据存储在数据库中。要获得更多帮助,请参阅本页的代码 echo "<table border='0' width='100%' id='table-3'> <tr><td>#</td><t
echo "<table border='0' width='100%' id='table-3'>
<tr><td>#</td><td>Title</td><td>Replies</td><td>Post Date</td></tr>";
$sql = mysql_query("SELECT * FROM topics ORDER BY id DESC");
while ($row = mysql_fetch_array($sql)) {
$replies = mysql_num_rows(mysql_query("SELECT * FROM replies WHERE
topic_id='" . $row['id'] . "'"));
echo "<tr><td>" . $row['id'] . "</td><td><a href='show_topic.php?id=" . $row['id']
. "'> <b>" . $row['title'] . "</b></a></td><td>" . $replies . "</td><td>" .
date("d M Y", $row['date']) . " </td></tr>";
}
使用
如果你在寻找完整的单词,这是一个简单的函数,将。。。如果内容长于指定的字数:
function excerpt($content,$numberOfWords = 10){
$contentWords = substr_count($content," ") + 1;
$words = explode(" ",$content,($numberOfWords+1));
if( $contentWords > $numberOfWords ){
$words[count($words) - 1] = '...';
}
$excerpt = join(" ",$words);
return $excerpt;
}
然后,您只需使用以下命令调用函数:
excerpt($row['description'],10)
如果有空格,此函数将在不剪切任何单词的情况下修剪文本。否则,它会在长度限制后立即将其切断
function trim_text($input, $length, $ellipses = true)
{
if (strlen($input) <= $length) {
return $input;
}
// find last space within length
$last_space = strrpos(substr($input, 0, $length), ' ');
if ($last_space === FALSE) {
$last_space = $length;
}
$trimmed_text = substr($input, 0, $last_space);
// add ellipses
if ($ellipses) {
$trimmed_text .= '...';
}
return $trimmed_text;
}
试着这样做:
<?php
$news = strip_tags($row['apal_news']);
$snews = substr($news,0,50);
echo $news;
?>
phpi中的substr函数将使用mysql函数SUBSTRING_INDEXDagon,您能在下面解释一下吗?相关:您想剥离contentNice中的标签,请帮助我
<?php
$news = strip_tags($row['apal_news']);
$snews = substr($news,0,50);
echo $news;
?>