将字符串截断为字符串的前 n 个字符,如果删除了任何字符,则添加三个点
PHP 中如何获取字符串的前 n 个字符?将字符串修剪为特定数量的字符并在需要时附加“...”的最快方法是什么?
How can I get the first n characters of a string in PHP? What's the fastest way to trim a string to a specific number of characters, and append '...' if needed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(21)
如果对截断字符串的长度没有硬性要求,可以使用它来截断并防止剪切最后一个单词:
在这种情况下,
$preview
将是“知识是每个人的自然权利”
。实时代码示例:
http://sandbox.onlinephpfunctions.com/code/25484a8b687d1f5ad93f62082b6379662a6b 4713
If there is no hard requirement on the length of the truncated string, one can use this to truncate and prevent cutting the last word as well:
In this case,
$preview
will be"Knowledge is a natural right of every human being"
.Live code example:
http://sandbox.onlinephpfunctions.com/code/25484a8b687d1f5ad93f62082b6379662a6b4713
此解决方案不会剪切单词,它会在第一个空格后添加三个点。
我编辑了 @Raccoon29 解决方案,并将所有函数替换为 mb_ 函数,以便这适用于所有语言,例如阿拉伯语
this solution will not cut words, it will add three dots after the first space.
I edited @Raccoon29 solution and I replaced all functions with mb_ functions so that this will work for all languages such as arabic
$宽度= 10;
或使用自动换行
$width = 10;
or with wordwrap
substr() 是最好的,您还需要检查字符串的长度,第一个
自动换行不会修剪字符串,只需将其拆分...
substr() would be best, you'll also want to check the length of the string first
wordwrap won't trim the string down, just split it up...
我不确定这是否是最快的解决方案,但看起来它是最短的解决方案:
PS 请参阅此处的一些示例 https:// /stackoverflow.com/a/17852480/131337
I'm not sure if this is the fastest solution, but it looks like it is the shortest one:
P.S. See some examples here https://stackoverflow.com/a/17852480/131337
最好像这样抽象你的代码(注意限制是可选的,默认为 10):
It's best to abstract you're code like so (notice the limit is optional and defaults to 10):
要在函数内创建(用于重复使用)和动态有限长度,请使用:
To create within a function (for repeat usage) and dynamical limited length, use:
我为此开发了一个函数,
您只需使用字符串和 limite 调用该函数
例如:
str_short("哈哈哈哈哈哈",5)
;它会切断你的字符串并在末尾添加“...”
:)
I developed a function for this use
you just call the function with string and limite
eg:
str_short("hahahahahah",5)
;it will cut of your string and add "..." at the end
:)
这就是我所做的
,其中 $num 代表字符数,$tt 代表用于操作的字符串。
This is what i do
where $num stands for number of chars, and $tt the string for manipulation.
我使用的功能:
查看它实际操作。
The function I used:
See it in action.
Laravel 6+ 版本中已经有一个辅助方法。你可以简单地使用它。
它为您提供类似的输出
有关更多详细信息,请查看 laravel 官方文档: https: //laravel.com/docs/8.x/helpers#method-str-limit
It already has a helper method for it in Laravel 6+ versions. You could simply use that.
which gives you output like
For more detail please check laravel official document: https://laravel.com/docs/8.x/helpers#method-str-limit
这个函数可以完成工作而不会在中间中断单词
This function do the job without breaking words in the middle
使用子字符串
http://php.net/manual/en/function.substr.php< /a>
Use substring
http://php.net/manual/en/function.substr.php
codeigniter 框架包含一个用于此目的的帮助程序,称为“文本帮助程序”。以下是 codeigniter 用户指南中适用的一些文档: http://codeigniter.com/user_guide/helpers/text_helper .html
(只需阅读 word_limiter 和 character_limiter 部分)。
这是与您的问题相关的两个函数:
和
The codeigniter framework contains a helper for this, called the "text helper". Here's some documentation from codeigniter's user guide that applies: http://codeigniter.com/user_guide/helpers/text_helper.html
(just read the word_limiter and character_limiter sections).
Here's two functions from it relevant to your question:
And
如果你想小心地不要分割单词,你可以执行以下操作,
如果 $str 短于 $n_chars 返回它不变。
如果 $str 等于 $n_chars 也按原样返回。
如果 $str 比 $n_chars 长,那么它会查找下一个要剪切的空格,或者(如果直到最后没有更多空格)$str 会在 $n_chars 处被粗暴地剪切。
注意:请注意,此方法将删除 HTML 中的所有标签。
If you want to cut being careful to don't split words you can do the following
if $str is shorter than $n_chars returns it untouched.
If $str is equal to $n_chars returns it as is as well.
if $str is longer than $n_chars then it looks for the next space to cut or (if no more spaces till the end) $str gets cut rudely instead at $n_chars.
NOTE: be aware that this method will remove all tags in case of HTML.
如果您需要控制字符串字符集,多字节扩展会派上用场。
The Multibyte extension can come in handy if you need control over the string charset.
有时,您需要将字符串限制为最后一个完整单词,即:您不希望最后一个单词被破坏,而是在倒数第二个单词处停止。
例如:
我们需要将“This is my String”限制为 6 个字符,但我们希望它不是“This i...”,而是“This...”,即我们将跳过最后一个单词中损坏的字母。
唷,我不擅长解释,这是代码。
sometimes, you need to limit the string to the last complete word ie: you don't want the last word to be broken instead you stop with the second last word.
eg:
we need to limit "This is my String" to 6 chars but instead of 'This i..." we want it to be 'This..." ie we will skip that broken letters in the last word.
phew, am bad at explaining, here is the code.
自 PHP 4.0.6 版本起,此功能已内置到 PHP 中。 查看文档。
请注意,
trimmarker
(上面的省略号)包含在截断的长度中。This functionality has been built into PHP since version 4.0.6. See the docs.
Note that the
trimmarker
(the ellipsis above) are included in the truncated length.更新:
基于检查长度的建议(并确保修剪和未修剪字符串的长度相似):
因此您将得到最多 13 个字符的字符串; 13 个(或更少)普通字符或 10 个字符后跟“...”
更新 2:
或作为函数:
更新 3:
自从我写这个答案以来已经有一段时间了,我实际上不再使用这个代码。我更喜欢这个函数,它可以防止使用
wordwrap
函数破坏单词中间的字符串:Update:
Based on suggestion for checking length (and also ensuring similar lengths on trimmed and untrimmed strings):
So you will get a string of max 13 characters; either 13 (or less) normal characters or 10 characters followed by '...'
Update 2:
Or as function:
Update 3:
It's been a while since I wrote this answer and I don't actually use this code any more. I prefer this function which prevents breaking the string in the middle of a word using the
wordwrap
function: