Home > Backend Development > PHP Problem > What to do if the php string is too long

What to do if the php string is too long

coldplay.xixi
Release: 2023-03-05 18:54:01
Original
3827 people have browsed it

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.

What to do if the php string is too long

[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(&#39;mb_substr&#39;)){
            $newstr = mb_substr($str, 0, $length, &#39;utf-8&#39;);
        }elseif (function_exists(&#39;iconv_substr&#39;)){
            $newstr = iconv_substr($str, 0, $length, &#39;utf-8&#39;);
        }else{
            //$newstr = trim_right(substr($str, 0, $length));
            $newstr = substr($str, 0, $length);
        }
        if ($append && $str != $newstr){
            $newstr .= &#39;...&#39;;
        }
        return $newstr;
    }
}
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template