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) < $start+1)<br>
{<br>
return '';<br>
}<br>
preg_match_all("/./su", $str, $ar);<br>
$str = '';<br>
$tstr = '';</p> <p> //为了兼容mysql4.1以下版本,与数据库varchar一致,这里使用按字节截取<br>
for($i=0; isset($ar[$i]); $i++)<br>
{<br>
if(strlen($tstr) < $start)<br>
{<br>
$tstr .= $ar[$i];<br>
}<br>
else<br>
{<br>
if(strlen($str) < $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) < $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) < $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));</p> <p>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 < $startdd+1)<br>
{<br>
return '';<br>
}<br>
if($str_len < $startdd + $slen || $slen==0)<br>
{<br>
$slen = $str_len - $startdd;<br>
}<br>
$enddd = $startdd + $slen - 1;<br>
for($i=0;$i<$str_len;$i++) { if($startdd==0) { $restr .= $c; } else if($i > $startdd)<br>
{<br>
$restr .= $c;<br>
}</p> <p> if(ord($str[$i])>0x80)<br>
{<br>
if($str_len>$i+1)<br>
{<br>
$c = $str[$i].$str[$i+1];<br>
}<br>
$i++;<br>
}<br>
else<br>
{<br>
$c = $str[$i];<br>
}</p> <p> if($i >= $enddd)<br>
{<br>
if(strlen($restr)+strlen($c)>$slen)<br>
{<br>
break;<br>
}<br>
else<br>
{<br>
$restr .= $c;<br>
break;<br>
}<br>
}<br>
}<br>
return $restr;<br>
}</p> <p>function cn_substr_utf8($str, $length, $start=0)<br>
{<br>
if(strlen($str) < $start+1)<br>
{<br>
return '';<br>
}<br>
preg_match_all("/./su", $str, $ar);</p> <p> $str = '';<br>
$tstr = '';</p> <p> //为了兼容mysql4.1以下版本,与数据库varchar一致,这里使用按字节截取<br>
for($i=0; isset($ar[$i]); $i++)<br>
{<br>
if(strlen($tstr) < $start)<br>
{</p> <p> $tstr .= $ar[$i];<br>
}<br>
else<br>
{</p> <p> if(strlen($str) < $length + strlen($ar[$i]) )<br>
{</p> <p> $str .= $ar[$i];<br>
}<br>
else<br>
{</p> <p> break;<br>
}<br>
}<br>
}<br>
return $str;<br>
}</p> <p>function cn_substr_utf82($str, $length, $start=0)<br>
{<br>
if(strlen($str) < $start+1)<br>
{<br>
return '';<br>
}<br>
preg_match_all("/./su", $str, $ar);</p> <p> $str = '';<br>
$tstr = '';</p> <p> //为了兼容mysql4.1以下版本,与数据库varchar一致,这里使用按字节截取<br>
for($i=0; isset($ar[$i]); $i++)<br>
{<br>
if(strlen($tstr) < $start)<br>
{</p> <p> $tstr .= $ar[$i];<br>
}<br>
else<br>
{</p> <p> if(strlen($str) < $length + strlen($ar[$i]) -1 ) // phpsir 加了 -1 <br>
{</p> <p> $str .= $ar[$i];<br>
}<br>
else<br>
{</p> <p> break;<br>
}<br>
}<br>
}<br>
return $str;<br>
}</div>
<div>
</div>
頁:
[1]