Home  >  Article  >  php教程  >  解决中英文字符串长度问题函数

解决中英文字符串长度问题函数

WBOY
WBOYOriginal
2016-06-13 12:33:53974browse

复制代码 代码如下:


function strSplit($s, $len) {
    $end = '…';
    $result = '';
    $strLen = strlen($s);
    if ($strLen         return $s;
    }
    $len -= 2;
    for ($i=0; $i        $c = $s[$i];
        if (ord($c)             $result .= $c;
        } elseif ($i+1            $result .= $s[$i++] . $s[$i];
        }
    }
    return ($i }

echo strSplit('1234567', 10), '
';
echo strSplit('1234567890', 10), '
';
echo strSplit('1234中文567890abcdefghijkl', 10), '
';
echo strSplit('全部都是中文', 10), '
';
echo strSplit('全a部b都c是d中e文', 10), '
'; 

输出:
1234567
1234567890
1234中文…
全部都是…
全a部b都… 

Statement:
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