思儒爸 發表於 2023-11-19 00:00:00

dedecms 的cn_substr_utf8字符串截取函数商榷

<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
        在dedecms里面 cn_substr_utf8 函数是这样的</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
         </p>
<div class="msgheader" style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
        <div class="right">
                <span><u>复制代码</u></span>
</div>
        代码如下:</div>
<div class="msgborder" id="phpcode8" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
        <br>
        /**<br>
        * utf-8中文截取,单字节截取模式<br>
        *<br>
        * @access public<br>
        * @param string $str 需要截取的字符串<br>
        * @param int $slen 截取的长度<br>
        * @param int $startdd 开始标记处<br>
        * @return string<br>
        */<br>
        if ( ! function_exists('cn_substr_utf8'))<br>
        {<br>
        function cn_substr_utf8($str, $length, $start=0)<br>
        {<br>
        if(strlen($str) &lt; $start+1)<br>
        {<br>
        return '';<br>
        }<br>
        preg_match_all("/./su", $str, $ar);<br>
        $str = '';<br>
        $tstr = '';&lt;/p&gt; &lt;p&gt; //为了兼容mysql4.1以下版本,与数据库varchar一致,这里使用按字节截取<br>
        for($i=0; isset($ar[$i]); $i++)<br>
        {<br>
        if(strlen($tstr) &lt; $start)<br>
        {<br>
        $tstr .= $ar[$i];<br>
        }<br>
        else<br>
        {<br>
        if(strlen($str) &lt; $length + strlen($ar[$i]) )<br>
        {<br>
        $str .= $ar[$i];<br>
        }<br>
        else<br>
        {<br>
        break;<br>
        }<br>
        }<br>
        }<br>
        return $str;<br>
        }<br>
        }</div>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
         </p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
        其中</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
         </p>
<div class="msgheader" style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
        <div class="right">
                <span><u>复制代码</u></span>
</div>
        代码如下:</div>
<div class="msgborder" id="phpcode9" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
        <br>
        if(strlen($str) &lt; $length + strlen($ar[$i]) )</div>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
         </p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
        一行可能会造成截取后多了一个字符,可以考虑改为</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
         </p>
<div class="msgheader" style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
        <div class="right">
                <span><u>复制代码</u></span>
</div>
        代码如下:</div>
<div class="msgborder" id="phpcode10" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
        <br>
        if(strlen($str) &lt; $length + strlen($ar[$i]) -1 )</div>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
         </p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
        测试代码如下</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
         </p>
<div class="msgheader" style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
        <div class="right">
                <span><u>复制代码</u></span>
</div>
        代码如下:</div>
<div class="msgborder" id="phpcode11" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
        <br>
        $f = "你好fasdfa你fasdf#e#";<br>
        $pos = strpos($f,'#e#');<br>
        var_dump($pos);<br>
        var_dump(cn_substr_utf8($f,$pos));<br>
        var_dump(cn_substr_utf82($f,$pos));&lt;/p&gt; &lt;p&gt;function cn_substr($str, $slen, $startdd=0)<br>
        {<br>
        global $cfg_soft_lang;<br>
        if($cfg_soft_lang=='utf-8')<br>
        {<br>
        return cn_substr_utf8($str, $slen, $startdd);<br>
        }<br>
        $restr = '';<br>
        $c = '';<br>
        $str_len = strlen($str);<br>
        if($str_len &lt; $startdd+1)<br>
        {<br>
        return '';<br>
        }<br>
        if($str_len &lt; $startdd + $slen || $slen==0)<br>
        {<br>
        $slen = $str_len - $startdd;<br>
        }<br>
        $enddd = $startdd + $slen - 1;<br>
        for($i=0;$i&lt;$str_len;$i++) { if($startdd==0) { $restr .= $c; } else if($i &gt; $startdd)<br>
        {<br>
        $restr .= $c;<br>
        }&lt;/p&gt; &lt;p&gt; if(ord($str[$i])&gt;0x80)<br>
        {<br>
        if($str_len&gt;$i+1)<br>
        {<br>
        $c = $str[$i].$str[$i+1];<br>
        }<br>
        $i++;<br>
        }<br>
        else<br>
        {<br>
        $c = $str[$i];<br>
        }&lt;/p&gt; &lt;p&gt; if($i &gt;= $enddd)<br>
        {<br>
        if(strlen($restr)+strlen($c)&gt;$slen)<br>
        {<br>
        break;<br>
        }<br>
        else<br>
        {<br>
        $restr .= $c;<br>
        break;<br>
        }<br>
        }<br>
        }<br>
        return $restr;<br>
        }&lt;/p&gt; &lt;p&gt;function cn_substr_utf8($str, $length, $start=0)<br>
        {<br>
        if(strlen($str) &lt; $start+1)<br>
        {<br>
        return '';<br>
        }<br>
        preg_match_all("/./su", $str, $ar);&lt;/p&gt; &lt;p&gt; $str = '';<br>
        $tstr = '';&lt;/p&gt; &lt;p&gt; //为了兼容mysql4.1以下版本,与数据库varchar一致,这里使用按字节截取<br>
        for($i=0; isset($ar[$i]); $i++)<br>
        {<br>
        if(strlen($tstr) &lt; $start)<br>
        {&lt;/p&gt; &lt;p&gt; $tstr .= $ar[$i];<br>
        }<br>
        else<br>
        {&lt;/p&gt; &lt;p&gt; if(strlen($str) &lt; $length + strlen($ar[$i]) )<br>
        {&lt;/p&gt; &lt;p&gt; $str .= $ar[$i];<br>
        }<br>
        else<br>
        {&lt;/p&gt; &lt;p&gt; break;<br>
        }<br>
        }<br>
        }<br>
        return $str;<br>
        }&lt;/p&gt; &lt;p&gt;function cn_substr_utf82($str, $length, $start=0)<br>
        {<br>
        if(strlen($str) &lt; $start+1)<br>
        {<br>
        return '';<br>
        }<br>
        preg_match_all("/./su", $str, $ar);&lt;/p&gt; &lt;p&gt; $str = '';<br>
        $tstr = '';&lt;/p&gt; &lt;p&gt; //为了兼容mysql4.1以下版本,与数据库varchar一致,这里使用按字节截取<br>
        for($i=0; isset($ar[$i]); $i++)<br>
        {<br>
        if(strlen($tstr) &lt; $start)<br>
        {&lt;/p&gt; &lt;p&gt; $tstr .= $ar[$i];<br>
        }<br>
        else<br>
        {&lt;/p&gt; &lt;p&gt; if(strlen($str) &lt; $length + strlen($ar[$i]) -1 ) // phpsir 加了 -1 <br>
        {&lt;/p&gt; &lt;p&gt; $str .= $ar[$i];<br>
        }<br>
        else<br>
        {&lt;/p&gt; &lt;p&gt; break;<br>
        }<br>
        }<br>
        }<br>
        return $str;<br>
        }</div>
<div>
         </div>
頁: [1]
查看完整版本: dedecms 的cn_substr_utf8字符串截取函数商榷