【发布时间】:2015-11-17 09:36:57
【问题描述】:
我只想在我的页面上显示已上传到数据库的 '$output' 的前 50 个字符,最简单的方法是什么?如果 $output 包含超过 50 个字符,我希望文本以它可以不超过 50 个字符的最后一个单词结尾,然后放一个“...”之类的东西,就像“阅读更多”部分一样。
PHP:
<?php
require_once("nbbc/nbbc.php");
$bbcode = new BBCode;
$sql = "SELECT * FROM posts ORDER BY id DESC";
$res = mysqli_query($dbCon, $sql) or die(mysqli_error($dbCon));
$posts = "";
if(mysqli_num_rows($res) > 0) {
while($row = mysqli_fetch_assoc($res)) {
$id = $row['id'];
$title = $row['title'];
$subtitle = $row['subtitle'];
$content = $row['content'];
$date = $row['date'];
$output = $bbcode->Parse($content);
$posts .= "<div id='post'><h2><a href='view_post.php?pid=$id' target='_blank'>$title</a></h2><h3 id='subtitle'>$subtitle</h3><h3 id='datethingy'><span style='color: #457F2C;'>Last updated:</span> $date</h3><p>$output</p></div>";
}
echo $posts;
}else {
echo "There are no posts to be displayed.";
}
?>
【问题讨论】:
-
你想要子字符串前 50 还是从 50 到 60 的子字符串或其他?
-
$content = $row['content'] 的输出是什么?问题可能出现在 BBCode 而不是数据库上。