php 限制字符串长度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3019285/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:26:18  来源:igfitidea点击:

Limit String Length

phpstring

提问by Belgin Fish

I'm looking for a way to limit a string in php and add on ... at the end if the string was too long.

我正在寻找一种方法来限制 php 中的字符串并在最后添加 ... 如果字符串太长。

回答by bramp

You can use something similar to the below:

您可以使用类似于以下内容的内容:

if (strlen($str) > 10)
   $str = substr($str, 0, 7) . '...';

回答by Sarath Sadasivan Pillai

From php 4.0.6, there is a function for the exact same thing

php 4.0.6 开始,有一个完全相同的功能

function mb_strimwidthcan be used for your requirement

功能mb_ strimwidth可用于您的要求

<?php
echo mb_strimwidth("Hello World", 0, 10, "...");
//Hello W...
?>

It does have more options though,here is the documentation for this mb_strimwidth

不过它确实有更多的选择,这里是这个mb_strimwidth的文档

回答by Sev

You can use the wordwrap()function then explode on newline and take the first part, if you don't want to split words.

wordwrap()如果您不想拆分单词,您可以使用该函数然后在换行符上爆炸并取第一部分。

$str = 'Stack Overflow is as frictionless and painless to use as we could make it.';
$str = wordwrap($str, 28);
$str = explode("\n", $str);
$str = $str[0] . '...';

Source: https://stackoverflow.com/a/1104329/1060423

来源:https: //stackoverflow.com/a/1104329/1060423

If you don't care about splitting words, then simply use the php substr function.

如果您不关心拆分单词,那么只需使用php substr 函数即可

echo substr($str, 0, 28) . '...';

回答by dlamblin

Do a little homework with the php online manual's string functions. You'll want to use strlenin a comparison setting, substrto cut it if you need to, and the concatenation operator with "..."or "&hellip;"

php在线手册的字符串函数做一点功课。您将需要strlen在比较设置中使用,substr根据需要进行剪切,以及带有"..."或的连接运算符"&hellip;"

回答by Naeem Ijaz

2nd argument is where to start string and 3rd argument how many characters you need to show

第二个参数是从哪里开始字符串和第三个参数你需要显示多少个字符

$title = "This is for testing string for get limit of string This is for testing string for get limit of string This is for testing string for get limit of string This is for testing string for get limit of string";
    echo substr($title,0,50);

回答by crmpicco

To truncate a string provided by the maximum limit without breaking a word use this:

要在不破坏单词的情况下截断由最大限制提供的字符串,请使用以下命令:

/**
 * truncate a string provided by the maximum limit without breaking a word
 * @param string $str
 * @param integer $maxlen
 * @return string
 */
public static function truncateStringWords($str, $maxlen): string
{
    if (strlen($str) <= $maxlen) return $str;

    $newstr = substr($str, 0, $maxlen);
    if (substr($newstr, -1, 1) != ' ') $newstr = substr($newstr, 0, strrpos($newstr, " "));

    return $newstr;
}

回答by geckob

In Laravel, there is a string util function for this, and it is implemented this way:

在 Laravel 中,有一个用于此的字符串 util 函数,它是这样实现的:

public static function limit($value, $limit = 100, $end = '...')
{
    if (mb_strwidth($value, 'UTF-8') <= $limit) {
        return $value;
    }

    return rtrim(mb_strimwidth($value, 0, $limit, '', 'UTF-8')).$end;
}

回答by PCMShaper

In another way to limit a string in php and add on readmore text or like '...' using below code

以另一种方式限制 php 中的字符串并使用以下代码添加 readmore 文本或类似 '...'

if (strlen(preg_replace('#^https?://#', '', $string)) > 30) { 
    echo substr(preg_replace('#^https?://#', '', $string), 0, 35).'&hellip;'; 
}

回答by Paul Caleb

$value = str_limit('This string is really really long.', 7);

$value = str_limit('这个字符串真的很长。', 7);

// This st...

// 这个...