The solution to the problem that the php string is too long: first intercept the length equal to 0 or greater than or equal to the length of the string, then return the string itself; then if the intercepted length is a negative number, then the intercepted length is equal to the length of the string Subtract the interception length; finally, if the absolute value of the interception length is greater than the length of the string itself, the interception length is the length of the string itself.
[Related learning recommendations: php programming (video)]
Solution to php string that is too long:
Use the method of judging the length of the string
if (! function_exists('mbSubStr')){ function mbSubStr($str, $length = 0, $append = true) { $str = trim($str); $strlength = strlen($str); if ($length == 0 || $length >= $strlength) { return $str; //截取长度等于0或大于等于本字符串的长度,返回字符串本身 }elseif ($length < 0){ //如果截取长度为负数 $length = $strlength + $length;//那么截取长度就等于字符串长度减去截取长度 if ($length < 0) { $length = $strlength;//如果截取长度的绝对值大于字符串本身长度,则截取长度取字符串本身的长度 } } if (function_exists('mb_substr')){ $newstr = mb_substr($str, 0, $length, 'utf-8'); }elseif (function_exists('iconv_substr')){ $newstr = iconv_substr($str, 0, $length, 'utf-8'); }else{ //$newstr = trim_right(substr($str, 0, $length)); $newstr = substr($str, 0, $length); } if ($append && $str != $newstr){ $newstr .= '...'; } return $newstr; } }
If you want to know more about programming learning, please pay attention to the php training column!
The above is the detailed content of What to do if the php string is too long. For more information, please follow other related articles on the PHP Chinese website!