我有以下代码限制一个html文本为500个字符:
if (strlen($text)>500) {
$message = substr($text, 0, 500);
echo $message;
}
else {
echo $text;
}
问题是有时代码在标记的中间切断,所以输出是:
...............................................Some text <a hre
我该如何解决这个问题?
最好的办法是不要剪掉消息。那么如何限制信息呢?答案:用CSS来限制。
echo '<div style="display:inline-block; overflow:hidden; width:500px; height:300px;">'.$message.'</div>';
现在你的消息没有被完全显示,但只有一部分,没有标签被剥离,所以没有这样的错误