如何用php切成一定长度的文本?
作者:互联网
我想从数据库中获取一些字段值,并在html上显示它们.
但是其中一些比div宽度长,所以我想砍的话,如果它们比30个字符长,则在它们后面添加3个点.
windows vs mac os x-> windows vs m...
threads about windows vista -> threads about win...
我怎样才能做到这一点?
解决方法:
如果您需要多次执行这种功能,请考虑以下功能:
function truncate($string, $limit, $break = '.', $pad = '...')
{
// return with no change if string is shorter than $limit
if(strlen($string) <= $limit) return $string;
// is $break present between $limit and the end of the string?
if(false !== ($breakpoint = strpos($string, $break, $limit)))
{
if($breakpoint < strlen($string) - 1)
{
$string = substr($string, 0, $breakpoint) . $pad;
}
}
return $string;
}
用法:
echo truncate($string, 30);
标签:string-concatenation,php 来源: https://codeday.me/bug/20191024/1919072.html