【发布时间】:2010-06-10 23:32:10
【问题描述】:
我正在寻找一种方法来限制 php 中的字符串,如果字符串太长,则在末尾添加 ...。
【问题讨论】:
我正在寻找一种方法来限制 php 中的字符串,如果字符串太长,则在末尾添加 ...。
【问题讨论】:
你可以使用类似下面的东西:
if (strlen($str) > 10)
$str = substr($str, 0, 7) . '...';
【讨论】:
mb_substr($utf8string,0,5,'UTF-8'); 这样可以避免切片长度错误。
从 php 4.0.6 开始,有一个完全相同的功能
函数 mb_srimwidth 可用于您的要求
<?php
echo mb_strimwidth("Hello World", 0, 10, "...");
//Hello W...
?>
不过,它确实有更多选项,这是mb_strimwidth 的文档
【讨论】:
如果您不想拆分单词,您可以使用wordwrap() 函数然后在换行符上展开并取第一部分。
$str = 'Stack Overflow is as frictionless and painless to use as we could make it.';
$str = wordwrap($str, 28);
$str = explode("\n", $str);
$str = $str[0] . '...';
来源:https://stackoverflow.com/a/1104329/1060423
如果您不关心拆分单词,那么只需使用php substr function。
echo substr($str, 0, 28) . '...';
【讨论】:
第二个参数是从哪里开始字符串,第三个参数是你需要显示多少个字符
$title = "This is for testing string for get limit of string This is for testing string for get limit of string This is for testing string for get limit of string This is for testing string for get limit of string";
echo substr($title,0,50);
【讨论】:
使用php online manual's string functions 做一些功课。
您需要在比较设置中使用strlen,如果需要,可以使用substr 进行剪切,以及使用"..." 或"&hellip;" 的连接运算符
【讨论】:
$string = (strlen($string) > 13) ? substr($string,0,10).'...' : $string;
或作为函数:
function truncate($string, $length, $dots = "...") {
return (strlen($string) > $length) ? substr($string, 0, $length - strlen($dots)) . $dots : $string;
}
我写这个答案已经有一段时间了,我实际上不再使用这个代码了。我更喜欢这个函数,它可以防止使用 wordwrap 函数破坏单词中间的字符串:
function truncate($string,$length=100,$append="…") {
$string = trim($string);
if(strlen($string) > $length) {
$string = wordwrap($string, $length);
$string = explode("\n", $string, 2);
$string = $string[0] . $append;
}
return $string;
}
【讨论】:
在 Laravel 中,有一个 string util 函数,它是这样实现的:
public static function limit($value, $limit = 100, $end = '...')
{
if (mb_strwidth($value, 'UTF-8') <= $limit) {
return $value;
}
return rtrim(mb_strimwidth($value, 0, $limit, '', 'UTF-8')).$end;
}
【讨论】:
要在不破坏单词的情况下截断最大限制提供的字符串,请使用:
/**
* truncate a string provided by the maximum limit without breaking a word
* @param string $str
* @param integer $maxlen
* @return string
*/
public static function truncateStringWords($str, $maxlen): string
{
if (strlen($str) <= $maxlen) return $str;
$newstr = substr($str, 0, $maxlen);
if (substr($newstr, -1, 1) != ' ') $newstr = substr($newstr, 0, strrpos($newstr, " "));
return $newstr;
}
【讨论】:
以另一种方式限制 php 中的字符串并使用下面的代码添加阅读更多文本或类似“...”
if (strlen(preg_replace('#^https?://#', '', $string)) > 30) {
echo substr(preg_replace('#^https?://#', '', $string), 0, 35).'…';
}
【讨论】:
$res = explode("\n",wordwrap('12345678910', 8, "...\n",true))[0];
// $res will be : "12345678..."
【讨论】:
function truncateString($string, $maxlength, $ellipsis = false){
if(mb_strlen($string) <= $maxlength){
return $string;
}
if(empty($ellipsis)){
$ellipsis = '';
}
if($ellipsis === true){
$ellipsis = '…';
}
$ellipsis_length = mb_strlen($ellipsis);
$maxlength = $maxlength - $ellipsis_length;
$string = trim(mb_substr($string, 0, $maxlength)) . $ellipsis;
return $string;
}
http://sandbox.onlinephpfunctions.com/code/968e2fd1e98b60828a12d6dc0b68ec38b3628757
【讨论】:
$value = str_limit('这个字符串真的很长。', 7);
// 这句...
【讨论】: