discuz截取字符串是基于php没有安装mb_substr等扩展运用的,并且如果截取中文字是二个字符作为计算。而ecshop是基于php的mb_substr,iconv_substr这两个扩展来运行的,中文都是以一个字符运算。
discuz 字符串截取
function cutstr($string, $length, $dot = ' ...') {
if(strlen($string) <= $length) {
return $string;
}
$pre = chr(1);
$end = chr(1);
$string = str_replace(array('&', '"', '<', '>'), array($pre.'&'.$end, $pre.'"'.$end, $pre.'<'.$end, $pre.'>'.$end), $string);
$strcut = '';
if(strtolower(CHARSET) == 'utf-8') {
$n = $tn = $noc = 0;
while($n < strlen($string)) {
$t = ord($string[$n]);
if($t == 9 || $t == 10 || (32 <= $t && $t <= 126)) {
$tn = 1; $n++; $noc++;
} elseif(194 <= $t && $t <= 223) {
$tn = 2; $n += 2; $noc += 2;
} elseif(224 <= $t && $t <= 239) {
$tn = 3; $n += 3; $noc += 2;
} elseif(240 <= $t && $t <= 247) {
$tn = 4; $n += 4; $noc += 2;
} elseif(248 <= $t && $t <= 251) {
$tn = 5; $n += 5; $noc += 2;
} elseif($t == 252 || $t == 253) {
$tn = 6; $n += 6; $noc += 2;
} else {
$n++;
}
if($noc >= $length) {
break;
}
}
if($noc > $length) {
$n -= $tn;
}
$strcut = substr($string, 0, $n);
} else {
for($i = 0; $i < $length; $i++) {
$strcut .= ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
}
}
$strcut = str_replace(array($pre.'&'.$end, $pre.'"'.$end, $pre.'<'.$end, $pre.'>'.$end), array('&', '"', '<', '>'), $strcut);
$pos = strrpos($strcut, chr(1));
if($pos !== false) {
$strcut = substr($strcut,0,$pos);
}
return $strcut.$dot;
}
$string = '我是中国人';
echo cutstr($string, 6, $dot = ' ...');//我是中。。。
ecshop 字符串截取
$string = '我是中国人';
echo sub_str($string, 3);//我是中。。。
function sub_str($str, $length = 0, $append = true)
{
$str = trim($str);
$strlength = strlen($str);
if ($length == 0 || $length >= $strlength)
{
return $str;
}
elseif ($length < 0)
{
$length = $strlength + $length;
if ($length < 0)
{
$length = $strlength;
}
}
if (function_exists('mb_substr'))
{
$newstr = mb_substr($str, 0, $length, 'utf-8');
}
elseif (function_exists('iconv_substr'))
{
$newstr = iconv_substr($str, 0, $length, 'utf-8');
}
else
{
//$newstr = trim_right(substr($str, 0, $length));
$newstr = substr($str, 0, $length);
}
if ($append && $str != $newstr)
{
$newstr .= '...';
}
return $newstr;
}
分享到:
相关推荐
下面先给出两个版本函数的源代码以及简单测试,最后我会给出一个实用性更强的字符串截取函数。需要注意的是:这里讨论的字符串截取问题都是针对UTF-8编码的中文字符串。 discuz版本 复制代码 代码如下: /** * ...
那教你一个办法,自己写两个函数,一个是记录操作日志函数,一个是还原日志数据到产品表函数,主要只针对产品属性组的修改和产品相册图的修改(前提,本身我这套系统做了二次开发,增加了产品的审核和取消修改功能)...
discuz和ecshop整合 discuz和ecshop会员整合
ecshop 函数列表大全
ecshop二次开发必备ecshop函数列表大全
ecshop函数列表 对于新手开发来说简直太有用哦
由于ECShop使用了str_replace函数做字符串替换,黑客可绕过单引号限制构造SQL注入语句。只要开启支付宝支付插件就能利用该漏洞获取网站数据,且不需要注册登入;ecshop的/includes/lib_insert.php文件中,对输入参数...
绝对实用的ECSHOP帮助文档 ECSHOP函数必不可少的帮助文件 ECSHOP开发 ECSHOP二次开发
该文档主要描述网店程序ECshop中使用的各种函数功能的说明和介绍,方便对ECshop的二次开发。
ecshop二次开发函数
ecshop 开发函数整理,免费共享,谢谢大家
PHPCMS DISCUZ ECSHOP整合步骤
十四、整理了一个 获取用户等级的函数 43 十五、商品页显示具体属性排序 44 十六、解决ecshop新建页面分页问题 49 十七、刚修改的伪静态分类URL 52 十八、ecshop批发销售 66 十九、网银支付直通插件 实现方法 69 二...
在做开发的时候,因为刚开始,所以找了这份文档。上面都是一些ecshop常用的开发函数。希望对刚开始进行ecshop开发的朋友有些帮助。
ECSHOP最新寺库网模板亲测可用。支持utf8和gbk两个版本。
包括ecshop模板制作参考手册; ecshop模板制作教程; ecshop数据库说明文档; ecshop函数列表..
ECShop函数手册
ECSHOP函数列表!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
ecshop的常用函数很全面,适合初学者学习,能有很大的提升