redfree 发表于 2007-6-4 09:06

怎样取一定长度的字符串?

我用了substr()取一定长度的字符串,一个汉字的长度可以用2,取5个汉字的长度就是 substr($str,0,10),但是如果其中有一个字母或数字的时候,就会在后面出现一个? ,这个应该怎样解决啊?

rainic 发表于 2007-6-4 09:31

用mb_substr

yuexiayiyu 发表于 2007-6-4 10:34

能举个例子吗?这函数不知道怎么用?[em04]

lmhllr 发表于 2007-6-4 11:13

<P>晕了,这个函数我在这里不只发了三次了,怎么就不喜欢搜索呢<BR><BR>[quote]<BR>&lt;?php<BR>function strSplit($s, $len) {<BR>    $end = '…';<BR>    $result = '';<BR>    $strLen = strlen($s);<BR>    if ($strLen &lt;= $len) {<BR>        return $s;<BR>    }<BR>    $len -= 2;<BR>    for ($i=0; $i&lt;$len &amp;&amp; $i&lt;$strLen; $i++) {<BR>        $c = $s[$i];<BR>        if (ord($c) &lt; 0x80) {<BR>            $result .= $c;<BR>        } elseif ($i+1&lt;$len) {<BR>            $result .= $s[$i++] . $s[$i];<BR>        }<BR>    }<BR>    return ($i &lt; $strLen) ? ($result . $end) : $result;<BR>}</P>
<P>echo strSplit('1234567', 10), '&lt;br /&gt;';<BR>echo strSplit('1234567890', 10), '&lt;br /&gt;';<BR>echo strSplit('1234中文567890abcdefghijkl', 10), '&lt;br /&gt;';<BR>echo strSplit('全部都是中文', 10), '&lt;br /&gt;';<BR>echo strSplit('全a部b都c是d中e文', 10), '&lt;br /&gt;'; <BR>?&gt;<BR>[/quote]</P>

页: [1]

编程论坛