php,统计字符数并删除超过140个字符的内容

发布于 2024-11-07 01:41:59 字数 299 浏览 3 评论 0原文

我需要一个 PHP 函数来计算短语的字符数。如果短语长度超过“140”个字符,则此函数应删除所有其他字符并在短语末尾添加三个点。 例如我们有。

$message= "I am what I am and you are what you are etc etc etc etc"

如果长度超过 140 个字符,那么

$message= "I am what I am and you are what you are..."

这可能吗?如何? 谢谢

I need a PHP function that will count the number of characters of a phrase. If the phrase is longer than "140" character then this function should delete all the other character and add three points at then end of the phrase.
So for example we have.

$message= "I am what I am and you are what you are etc etc etc etc"

IF this is longer than 140 characters, then

$message= "I am what I am and you are what you are..."

Is this possible? How?
Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

傲世九天 2024-11-14 01:41:59

如果你想“单词敏感”(即不在单词中间中断),你可以使用 wordwrap()

If you want to be "word sensitive" (i.e. not break in the middle of the word), you can use wordwrap().

挖个坑埋了你 2024-11-14 01:41:59
if(strlen($str) > 140){
   $str =  substr($str, 0, 140).'...';
}
if(strlen($str) > 140){
   $str =  substr($str, 0, 140).'...';
}
心碎的声音 2024-11-14 01:41:59

此变体将在必要的字符集(例如 utf-8)下正确工作,并且将尝试按空格剪切,以免破坏单词:

$charset = 'utf-8';
$len = iconv_strlen($str, $charset);
$max_len = 140;
$max_cut_len = 10;
if ($len > $max_len)
{
    $str = iconv_substr($str, 0, $max_len, $charset);
    $prev_space_pos = iconv_strrpos($str, ' ', $charset);
    if (($max_len-$prev_space_pos) < $max_cut_len) $str = iconv_substr($str, 0, $prev_space_pos, $charset);
    $str .= '...';
}

This variant will work correct with necessary charset (e.g. utf-8), and will try to cut by space, to not break words:

$charset = 'utf-8';
$len = iconv_strlen($str, $charset);
$max_len = 140;
$max_cut_len = 10;
if ($len > $max_len)
{
    $str = iconv_substr($str, 0, $max_len, $charset);
    $prev_space_pos = iconv_strrpos($str, ' ', $charset);
    if (($max_len-$prev_space_pos) < $max_cut_len) $str = iconv_substr($str, 0, $prev_space_pos, $charset);
    $str .= '...';
}
始于初秋 2024-11-14 01:41:59

那将是:

/**
 * trim up to 140 characters
 * @param string $str the string to shorten
 * @param int $length (optional) the max string length to return
 * @return string the shortened string
 */
function shorten($str, $length = 140) {
    if (strlen($str) > $length) {
        return substr($str, 0, $length).'...';
    }
    return $str;
}

/**
 * trim till last space before 140 characters
 * @param string $str the string to shorten
 * @param int $length (optional) the max string length to return
 * @return string the shortened string
 */
function smartShorten($str, $length = 140) {
    if (strlen($str) > $length) {
        if (false === ($pos = strrpos($str, ' ', $length))) { // no space found; cut till $length
            return substr($str, 0, $length).'...';
        }
        return substr($str, 0, strrpos($str, ' ', $length)).'...';
    }
    return $str;
}

That would be:

/**
 * trim up to 140 characters
 * @param string $str the string to shorten
 * @param int $length (optional) the max string length to return
 * @return string the shortened string
 */
function shorten($str, $length = 140) {
    if (strlen($str) > $length) {
        return substr($str, 0, $length).'...';
    }
    return $str;
}

/**
 * trim till last space before 140 characters
 * @param string $str the string to shorten
 * @param int $length (optional) the max string length to return
 * @return string the shortened string
 */
function smartShorten($str, $length = 140) {
    if (strlen($str) > $length) {
        if (false === ($pos = strrpos($str, ' ', $length))) { // no space found; cut till $length
            return substr($str, 0, $length).'...';
        }
        return substr($str, 0, strrpos($str, ' ', $length)).'...';
    }
    return $str;
}
追风人 2024-11-14 01:41:59

这是我经常使用的一个功能,

function shorten($str,$l = 30){
    return (strlen($str) > $l)? substr($str,0,$l)."...": $str;
}

您可以将默认长度更改为您想要的任何长度

This is a function that i use quite often

function shorten($str,$l = 30){
    return (strlen($str) > $l)? substr($str,0,$l)."...": $str;
}

you can change the default length to anything you want

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文