如何在 PHP 中使用 substr() 捕获完整的单词,按单词限制?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3538130/
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
How to capture complete words using substr() in PHP, limit by word?
提问by nectar
When I use substr($string,0,100)
, it gives first 100 characters. Sometimes it left the last word incomplete. That looks odd. Can I do limit by word rather than char?
当我使用 时substr($string,0,100)
,它给出前 100 个字符。有时它会使最后一个词不完整。那看起来很奇怪。我可以按单词而不是字符来限制吗?
回答by Mark Byers
If you just count the words the resulting sting could still be very long as a single "word" might have 30 characters or more. I would suggest instead truncating the text to 100 characters, except if this causes a word to be truncated then you should also remove the truncated part of the word. This is covered by this related question:
如果您只计算单词,结果的刺痛可能仍然很长,因为单个“单词”可能有 30 个或更多字符。我建议改为将文本截断为 100 个字符,除非这会导致单词被截断,那么您还应该删除单词的截断部分。这个相关问题涵盖了这一点:
How to Truncate a string in PHP to the word closest to a certain number of characters?
Using wordwrap
使用自动换行
$your_desired_width = 100;
if (strlen($string) > $your_desired_width)
{
$string = wordwrap($string, 100);
$i = strpos($string, "\n");
if ($i) {
$string = substr($string, 0, $i);
}
}
This is a modified versions of the answer here. if the input text could be very long you can add this line before the call to wordwrap to avoid wordwrap having to parse the entire text:
这是这里答案的修改版本。如果输入文本可能很长,您可以在调用 wordwrap 之前添加此行,以避免 wordwrap 必须解析整个文本:
$string = substr($string, 0, 101);
Using a regular expression(Source)
使用正则表达式(来源)
$string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, 100));
回答by Scott Evernden
$a = explode('|', wordwrap($string, 100, '|');
print $a[0];
回答by amateur barista
// Trim very long text to 120 characters. Add an ellipsis if the text is trimmed.
if(strlen($very_long_text) > 120) {
$matches = array();
preg_match("/^(.{1,120})[\s]/i", $very_long_text, $matches);
$trimmed_text = $matches[0]. '...';
}
回答by Govind Totla
Try a single line code
尝试单行代码
$string = preg_replace('/\s+?(\S+)?$/', '', substr($string, 0, $length));
回答by Peter
This is a solution very similar to that of Scott Evernden, but also works if the character | accidentially occurs in the string:
这是一个与 Scott Evernden 非常相似的解决方案,但如果角色 | 意外出现在字符串中:
$result = explode(chr(0), wordwrap($longtext, $len, chr(0)))[0];
回答by Shuhad zaman
i tried this simple one and it worked for me
我尝试了这个简单的方法,它对我有用
<?php echo substr($content, 0, strpos($content, ' ', 200)); ?>
回答by Matthew
I would do something like:
我会做这样的事情:
<?php
function cut_text($text, $len)
{
for ($i = 0; $i < 10; ++$i)
{
$c = $text[$len + $i];
if ($c == ' ' || $c == "\t" || $c == "\r" || $c == "\n" || $c == '-')
break;
}
if ($i == 10) $i = 0;
return rtrim(substr($text, 0, $len + $i));
}
echo cut_text("Hello, World!", 3)."\n";
?>
Just start at some point ($len) and move forward a certain number of characters ($i) looking for a breaking character (e.g., whitespace). You could also look backward (-$i) or in both directions, depending on what you are looking for.
只需从某个点 ($len) 开始并向前移动一定数量的字符 ($i) 以寻找中断字符(例如,空格)。您也可以向后看 (-$i) 或双向看,这取决于您要查找的内容。