你好,溢出,
所以我正在创建一个顶级脚本,准备向公众发布,我被困在一个特定的主题上。
显示 A 数据库字段中的 X 量内容。
<?php echo $server_data['description']; ?>
如下图所示,显示全部金额不是一个好主意。
https://i.stack.imgur.com/9jA1z.png
我需要什么?我只希望它显示该字段的 150 个字符,而不是显示所有数据库字段。
最好
在从数据库中进行选择时限制字符,因为这会稍微提高性能。您可以使用 mysql LEFT()
函数限制选择的字符。 这是操作方法:
SELECT LEFT(description, 150), another_col FROM ......
试试这个:
$string = substr($server_data['description'], 0, 150);
substr()
只会返回一定数量的字符。如果你想要一定数量的单词,那么你可以使用以下内容:
<?php
function excerpt($content = '',$number_words = 125){
$content = strip_tags($content);
$contentWords = substr_count($content," ") + 1;
$words = explode(" ",$content,($number_words+1));
$excerpt = join(" ",$words);
return $excerpt;
}
echo excerpt($server_data['description'], 125);