有没有办法限制 php 中的文本,然后添加展开/折叠选项?
例如:
我有一个 500 个字符长的注释,如果我设置文本限制 ($notetlimit =100;),我将得到 100 个字符,之后,我想插入图像(圆圈、箭头等),这将单击时显示文本的其余部分并更改为另一个图像(例如向上箭头),然后单击时再次关闭文本的其余部分。有点像分页符。
这可能吗,还是我必须使用js来展开和折叠?
最佳答案
您可以使用此功能来限制文本...
function gen_string($string,$min=1000,$clean=true) {
$text = trim(strip_tags($string));
if(strlen($text)>$min) {
$blank = strpos($text,' ');
if($blank) {
# limit plus last word
$extra = strpos(substr($text,$min),' ');
$max = $min+$extra;
$r = substr($text,0,$max);
} else {
# if there are no spaces
$r = substr($text,0,$min).'.........';
}
} else {
# if original length is lower than limit
$r = $text;
}
return $r ;
}
其中 $string 是您将限制长度为 $min = 1000 的文本
现在要像 google plus 一样展开或折叠文本,我们可以使用 truncate.js 文件。
关于PHP 文本限制与展开和折叠选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9095458/