gpt4 book ai didi

php - 如何用php截取一定长度的文本?

转载 作者:可可西里 更新时间:2023-11-01 00:53:31 24 4
gpt4 key购买 nike

我想从数据库中获取一些字段值并将它们呈现在 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);

关于php - 如何用php截取一定长度的文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2166828/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com