在 PHP 中截断文本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9219795/
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-26 06:23:04  来源:igfitidea点击:

Truncating Text in PHP?

phptruncate

提问by realph

I'm trying to truncate some text in PHP and have stumbled across this method (http://theodin.co.uk/blog/development/truncate-text-in-php-the-easy-way.html), which judging by the comments seems like a great easy-to-implement solution. The problem is I don't know how to implement it :S.

我试图在 PHP 中截断一些文本,并偶然发现了这种方法(http://theodin.co.uk/blog/development/truncate-text-in-php-the-easy-way.html),它判断通过评论似乎是一个很好的易于实施的解决方案。问题是我不知道如何实现它:S。

Would someone mind pointing me in the direction of what to do to implement this? Any help whatsoever would be appreciated.

有人会介意我指出如何实现这一点吗?任何帮助将不胜感激。

Thanks in advance.

提前致谢。

回答by Kai Qing

The obvious thing to do is read the documentation.

显而易见的事情是阅读文档

But to help: substr($str, $start, $end);

但要帮助: substr($str, $start, $end);

$stris your text

$str是你的文字

$startis the character index to begin at. In your case, it is likely 0 which means the very beginning.

$start是开始的字符索引。在您的情况下,它可能是 0,这意味着一开始。

$endis where to truncate at. Suppose you wanted to end at 15 characters, for example. You would write it like this:

$end是截断的位置。例如,假设您想以 15 个字符结尾。你会这样写:

<?php

$text = "long text that should be truncated";
echo substr($text, 0, 15);

?>

and you would get this:

你会得到这个:

long text that 

makes sense?

说得通?

EDIT

编辑

The link you gave is a function to find the last white space after chopping text to a desired length so you don't cut off in the middle of a word. However, it is missing one important thing - the desired length to be passed to the function instead of always assuming you want it to be 25 characters. So here's the updated version:

您提供的链接是一个函数,用于在将文本剪切到所需长度后找到最后一个空格,这样您就不会在单词中间被切断。但是,它缺少一件重要的事情 - 传递给函数的所需长度,而不是总是假设您希望它是 25 个字符。所以这是更新的版本:

function truncate($text, $chars = 25) {
    if (strlen($text) <= $chars) {
        return $text;
    }
    $text = $text." ";
    $text = substr($text,0,$chars);
    $text = substr($text,0,strrpos($text,' '));
    $text = $text."...";
    return $text;
}

So in your case you would paste this function into the functions.php file and call it like this in your page:

因此,在您的情况下,您可以将此函数粘贴到 functions.php 文件中,并在您的页面中像这样调用它:

$post = the_post();
echo truncate($post, 100);

This will chop your post down to the last occurrence of a white space before or equal to 100 characters. Obviously you can pass any number instead of 100. Whatever you need.

这会将您的帖子减少到最后一次出现或等于 100 个字符之前的空格。显然,您可以传递任何数字而不是 100。无论您需要什么。

回答by thenetimp

$mystring = "this is the text I would like to truncate";

// Pass your variable to the function
$mystring = truncate($mystring);

// Truncated tring printed out;
echo $mystring;

//truncate text function
public function truncate($text) {

    //specify number fo characters to shorten by
    $chars = 25;

    $text = $text." ";
    $text = substr($text,0,$chars);
    $text = substr($text,0,strrpos($text,' '));
    $text = $text."...";
    return $text;
}

回答by FreudianSlip

$text="abc1234567890";

// truncate to 4 chars

echo substr(str_pad($text,4),0,4);

This avoids the problem of truncating a 4 char string to 10 chars .. (i.e. source is smaller than the required)

这避免了将 4 个字符的字符串截断为 10 个字符的问题..(即源小于所需)