2

有没有办法在 PHP 中修剪文本字符串,使其具有一定数量的字符?例如,如果我有字符串:

$string = "this is a string";

我怎么能修剪它说:

$newstring = "this is";

这是我到目前为止使用chunk_split()的,但它不起作用。任何人都可以改进我的方法吗?

function trimtext($text)
{
$newtext = chunk_split($text,15);
return $newtext;
}

我也看过这个问题,但我不是很明白。

4

10 回答 10

13
if (strlen($yourString) > 15) // if you want...
{
    $maxLength = 14;
    $yourString = substr($yourString, 0, $maxLength);
}

将完成这项工作。

看看这里

于 2013-04-29T12:35:37.710 回答
6

substr 将单词减半。此外,如果 word 包含 UTF8 字符,则它行为不端。所以最好使用mb_substr:

$string = mb_substr('word word word word', 0, 10, 'utf8').'...';

于 2014-11-24T10:01:42.743 回答
5

你没有说原因,而是想想你想要达到什么。这是一个逐字缩短字符串的函数,可以在末尾添加或不添加省略号:

function limitStrlen($input, $length, $ellipses = true, $strip_html = true) {
    //strip tags, if desired
    if ($strip_html) {
        $input = strip_tags($input);
    }

    //no need to trim, already shorter than trim length
    if (strlen($input) <= $length) {
        return $input;
    }

    //find last space within length
    $last_space = strrpos(substr($input, 0, $length), ' ');
    if($last_space !== false) {
        $trimmed_text = substr($input, 0, $last_space);
    } else {
        $trimmed_text = substr($input, 0, $length);
    }
    //add ellipses (...)
    if ($ellipses) {
        $trimmed_text .= '...';
    }

    return $trimmed_text;
}
于 2013-04-29T12:54:23.590 回答
4
function trimtext($text, $start, $len)
{
    return substr($text, $start, $len);
}

您可以像这样调用该函数:

$string = trimtext("this is a string", 0, 10);

将返回:

This is a

于 2013-04-29T12:36:25.983 回答
3

substr让我们取一部分字符串,该部分由您需要的字符组成。

于 2013-04-29T12:36:07.160 回答
3

你可以用这个

substr()

获取子字符串的函数

于 2013-04-29T12:36:17.017 回答
2

If you want to get a string with a certain number of characters you can use substr, i.e.

$newtext = substr($string,0,$length); 

where $length is the given length of the new string.

于 2013-04-29T12:38:22.227 回答
0

If you want an abstract for the first 10 words (you can use html in $text, before script there is strip_tags) use this code:

preg_match('/^([^.!?\s]*[\.!?\s]+){0,10}/', strip_tags($text), $abstract);
echo $abstract[0];
于 2013-04-29T12:51:29.753 回答
0

我的函数有一些长度,但我喜欢使用它。我将字符串 int 转换为数组。

function truncate($text, $limit){
    //Set Up
    $array = [];
    $count = -1;
    //Turning String into an Array
    $split_text = explode(" ", $text);
    //Loop for the length of words you want
    while($count < $limit - 1){
      $count++;
      $array[] = $split_text[$count];
    }
    //Converting Array back into a String
    $text = implode(" ", $array);

    return $text." ...";

  }

或者,如果文本来自编辑器并且您想要去除 HTML 标记。

function truncate($text, $limit){
    //Set Up
    $array = [];
    $count = -1;
    $text = filter_var($text, FILTER_SANITIZE_STRING);

    //Turning String into an Array
    $split_text = preg_split('/\s+/', $text);
    //Loop for the length of words you want
    while($count < $limit){
      $count++;
      $array[] = $split_text[$count];
    }

    //Converting Array back into a String
    $text = implode(" ", $array);

    return $text." ...";

  }
于 2016-09-22T17:54:03.203 回答
0

省略号 (...) 仅在更长的情况下使用 - 并注意特殊的语言特定字符:

mb_strlen($text,'UTF-8') > 60 ? mb_substr($text, 0, 60,'UTF-8') . "…&quot; : $text;
于 2021-11-26T13:48:28.090 回答