我有一个用PHP编写的代码片段,它从数据库中提取一个文本块,并将其发送到网页上的一个小部件。原文可以是一篇很长的文章,也可以是一两个短句;但是对于这个小部件,我不能显示超过200个字符。我可以使用substr()在200个字符处切断文本,但结果将在单词中间切断——我真正想要的是在200个字符前的最后一个单词的末尾切断文本。
当前回答
你可以用这个:
function word_shortener($text, $words=10, $sp='...'){
$all = explode(' ', $text);
$str = '';
$count = 1;
foreach($all as $key){
$str .= $key . ($count >= $words ? '' : ' ');
$count++;
if($count > $words){
break;
}
}
return $str . (count($all) <= $words ? '' : $sp);
}
例子:
word_shortener("Hello world, this is a text", 3); // Hello world, this...
word_shortener("Hello world, this is a text", 3, ''); // Hello world, this
word_shortener("Hello world, this is a text", 3, '[read more]'); // Hello world, this[read more]
Edit
工作原理:
1. 从输入文本中打开空格:
$all = explode(' ', $text);
例如,如果$text将是"Hello world",那么$all是一个带有爆炸值的数组:
“你好”,“世界”)
2. 对于每个单词:
选择展开文本中的每个元素:
foreach($all as $key){...
将当前单词($key)附加到$str,如果它是最后一个单词,则添加空格:
$str .= $key . ($count >= $words ? '' : ' ');
然后给$count加上1,检查它是否大于max limit($words)打破循环:
if($count > $words){
break;
}
只有当最终文本小于输入文本时,才返回$str和分隔符($sp):
return $str . (count($all) <= $words ? '' : $sp);
其他回答
/*
Cut the string without breaking any words, UTF-8 aware
* param string $str The text string to split
* param integer $start The start position, defaults to 0
* param integer $words The number of words to extract, defaults to 15
*/
function wordCutString($str, $start = 0, $words = 15 ) {
$arr = preg_split("/[\s]+/", $str, $words+1);
$arr = array_slice($arr, $start, $words);
return join(' ', $arr);
}
用法:
$input = 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna liqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.';
echo wordCutString($input, 0, 10);
这将输出前10个单词。
preg_split函数用于将字符串拆分为子字符串。要分割字符串的边界是使用正则表达式模式指定的。
Preg_split函数需要4个参数,但目前只有前3个与我们相关。
第一个参数-模式 第一个参数是要拆分字符串的正则表达式模式。在本例中,我们希望跨单词边界分割字符串。因此,我们使用预定义的字符类\s来匹配空格字符,如空格、制表符、回车和换行。
第二个参数-输入字符串 第二个参数是我们要分割的长文本字符串。
第三个参数-限制 第三个参数指定应该返回的子字符串的数量。如果将限制设置为n,则preg_split将返回一个包含n个元素的数组。前n-1个元素将包含子字符串。最后(第n个)元素将包含字符串的其余部分。
我相信这是最简单的方法:
$lines = explode('♦♣♠',wordwrap($string, $length, '♦♣♠'));
$newstring = $lines[0] . ' • • •';
我正在使用特殊字符分割文本并剪切它。
我有一个函数,它几乎可以做你想要的,如果你做一些编辑,它将完全适合:
<?php
function stripByWords($string,$length,$delimiter = '<br>') {
$words_array = explode(" ",$string);
$strlen = 0;
$return = '';
foreach($words_array as $word) {
$strlen += mb_strlen($word,'utf8');
$return .= $word." ";
if($strlen >= $length) {
$strlen = 0;
$return .= $delimiter;
}
}
return $return;
}
?>
当我注意到wordwrap函数的$break参数时,产生了以下解决方案:
字符串$str [, int $width = 75[,字符串$break = "\n" [, bool $cut = false]])
下面是解决方案:
/**
* Truncates the given string at the specified length.
*
* @param string $str The input string.
* @param int $width The number of chars at which the string will be truncated.
* @return string
*/
function truncate($str, $width) {
return strtok(wordwrap($str, $width, "...\n"), "\n");
}
示例# 1。
print truncate("This is very long string with many chars.", 25);
上面的例子将输出:
This is very long string...
例# 2。
print truncate("This is short string.", 25);
上面的例子将输出:
This is short string.
你可以用这个:
function word_shortener($text, $words=10, $sp='...'){
$all = explode(' ', $text);
$str = '';
$count = 1;
foreach($all as $key){
$str .= $key . ($count >= $words ? '' : ' ');
$count++;
if($count > $words){
break;
}
}
return $str . (count($all) <= $words ? '' : $sp);
}
例子:
word_shortener("Hello world, this is a text", 3); // Hello world, this...
word_shortener("Hello world, this is a text", 3, ''); // Hello world, this
word_shortener("Hello world, this is a text", 3, '[read more]'); // Hello world, this[read more]
Edit
工作原理:
1. 从输入文本中打开空格:
$all = explode(' ', $text);
例如,如果$text将是"Hello world",那么$all是一个带有爆炸值的数组:
“你好”,“世界”)
2. 对于每个单词:
选择展开文本中的每个元素:
foreach($all as $key){...
将当前单词($key)附加到$str,如果它是最后一个单词,则添加空格:
$str .= $key . ($count >= $words ? '' : ' ');
然后给$count加上1,检查它是否大于max limit($words)打破循环:
if($count > $words){
break;
}
只有当最终文本小于输入文本时,才返回$str和分隔符($sp):
return $str . (count($all) <= $words ? '' : $sp);
推荐文章
- Printf与std::字符串?
- 不区分大小写的“in”
- 原则-如何打印出真正的sql,而不仅仅是准备好的语句?
- 如何从关联PHP数组中获得第一项?
- 我如何得到一个字符串的前n个字符而不检查大小或出界?
- PHP/MySQL插入一行然后获取id
- 我如何排序一个多维数组在PHP
- 如何在PHP中截断字符串最接近于一定数量的字符?
- PHP错误:“zip扩展名和unzip命令都没有,跳过。”
- Nginx提供下载。php文件,而不是执行它们
- Json_encode()转义正斜杠
- 如何在PHP中捕获cURL错误
- Ruby数组到字符串的转换
- 为什么在Java和。net中不能修改字符串?
- 如何要求一个分叉与作曲家?