kar*_*m79 126
function limit_text($text, $limit) {
if (str_word_count($text, 0) > $limit) {
$words = str_word_count($text, 2);
$pos = array_keys($words);
$text = substr($text, 0, $pos[$limit]) . '...';
}
return $text;
}
echo limit_text('Hello here is a long sentence blah blah blah blah blah hahahaha haha haaaaaa', 5);
Run Code Online (Sandbox Code Playgroud)
输出:
Hello here is a long ...
Run Code Online (Sandbox Code Playgroud)
nop*_*ole 29
将数字更改为下面3的数字20以获取前20个单词,或将其作为参数传递.下面演示了如何得到第3个字:(所以更改3以20更改默认值):
function first3words($s, $limit=3) {
return preg_replace('/((\w+\W*){'.($limit-1).'}(\w+))(.*)/', '${1}', $s);
}
var_dump(first3words("hello yes, world wah ha ha")); # => "hello yes, world"
var_dump(first3words("hello yes,world wah ha ha")); # => "hello yes,world"
var_dump(first3words("hello yes world wah ha ha")); # => "hello yes world"
var_dump(first3words("hello yes world")); # => "hello yes world"
var_dump(first3words("hello yes world.")); # => "hello yes world"
var_dump(first3words("hello yes")); # => "hello yes"
var_dump(first3words("hello")); # => "hello"
var_dump(first3words("a")); # => "a"
var_dump(first3words("")); # => ""
Run Code Online (Sandbox Code Playgroud)
Jac*_*nkr 13
截断到目标字符的最近前空格.演示
$str 要截断的字符串$chars 要剥离的字符数量可以被覆盖 $to_space$to_space boolean是否从$chars极限空间截断功能
function truncateString($str, $chars, $to_space, $replacement="...") {
if($chars > strlen($str)) return $str;
$str = substr($str, 0, $chars);
$space_pos = strrpos($str, " ");
if($to_space && $space_pos >= 0)
$str = substr($str, 0, strrpos($str, " "));
return($str . $replacement);
}
Run Code Online (Sandbox Code Playgroud)
样品
<?php
$str = "this is a string that is just some text for you to test with";
print(truncateString($str, 20, false) . "\n");
print(truncateString($str, 22, false) . "\n");
print(truncateString($str, 24, true) . "\n");
print(truncateString($str, 26, true, " :)") . "\n");
print(truncateString($str, 28, true, "--") . "\n");
?>
Run Code Online (Sandbox Code Playgroud)
this is a string tha...
this is a string that ...
this is a string that...
this is a string that is :)
this is a string that is--
Run Code Online (Sandbox Code Playgroud)
使用explode().
来自文档的示例.
// Example 1
$pizza = "piece1 piece2 piece3 piece4 piece5 piece6";
$pieces = explode(" ", $pizza);
echo $pieces[0]; // piece1
echo $pieces[1]; // piece2
Run Code Online (Sandbox Code Playgroud)
请注意,爆炸具有限制功能.所以你可以做点什么
$message = implode(" ", explode(" ", $long_message, 20));
Run Code Online (Sandbox Code Playgroud)
试试正则表达式.
你需要一些能匹配20个单词(或20个单词边界)的东西.
所以(我的正则表达式非常糟糕,如果不准确的话,请纠正我):
/(\w+\b){20}/
Run Code Online (Sandbox Code Playgroud)
以下是php中正则表达式的一些示例.
小智 7
简单且完全配备的truncate()方法:
function truncate($string, $width, $etc = ' ..')
{
$wrapped = explode('$trun$', wordwrap($string, $width, '$trun$', false), 2);
return $wrapped[0] . (isset($wrapped[1]) ? $etc : '');
}
Run Code Online (Sandbox Code Playgroud)
小智 5
它不是我自己的创作,它是以前帖子的修改.积分去karim79.
function limit_text($text, $limit) {
$strings = $text;
if (strlen($text) > $limit) {
$words = str_word_count($text, 2);
$pos = array_keys($words);
if(sizeof($pos) >$limit)
{
$text = substr($text, 0, $pos[$limit]) . '...';
}
return $text;
}
return $text;
}
Run Code Online (Sandbox Code Playgroud)
如果你在 Laravel 上编码use Illuminate\Support\Str
这是例子
Str::words($category->publication->title, env('WORDS_COUNT_HOME'), '...')
Run Code Online (Sandbox Code Playgroud)
希望这有帮助。