VPS参考测评推荐
专注分享VPS主机优惠信息
衡天云优惠活动
华纳云最新优惠促销活动
jtti最新优惠促销活动

如何通过dedecms重新定义cn_substr函数来更准确的截取字数?

locvps
主机参考:VPS测评参考推荐/专注分享VPS服务器优惠信息!若您是商家可以在本站进行投稿,查看详情!此外我们还提供软文收录、PayPal代付、广告赞助等服务,查看详情!
我们发布的部分优惠活动文章可能存在时效性,购买时建议在本站搜索商家名称可查看相关文章充分了解该商家!若非中文页面可使用Edge浏览器同步翻译!PayPal代付/收录合作

如何通过dedecms重新定义cn_substr函数来更准确的截取字数?

dedecms如何重新定义cn_substr函数截取的字数使其更准确?

不允许使用由dedecms的cn_substr()和cn_substr_utf8()截获的字符串ms。我通常使用cn_substr()的习惯,今天不想使用任何cn_substr_utf8(),但它现在仍然更准确。按照一个汉字用两个字节来称呼就可以了。

推荐学习:梦想编织cms

方法描述:

首先,找到\ include \ helpers \ string.helper.php并替换原来的33-102行代码(即定义cn_substr()函数的代码)。如果你害怕它,你可以先备份这个文件,亲爱的;

代码如下:

/* * *截取的字符串为中英文,中文字符为2个字节* * @ access public * @ paramstring $ str要截取的字符串* @param int $cutLen截取的长度* @param bool $cutSlashes,您确定要删除它吗\ * @param bool $addSlashes是由\ * @param string $oDot截取后添加的字符串,例如三个常用的点* @ param bool $ hasHtml,是否有html * @return string */ if(!function _ exists(‘cn _ substr‘){ function cn _ substr($ str,$cutLen,$oDot = null,$ hasHtml = false,$cutSlashes = false,$ add slashes = false){ global $ CFG _ soft _ lang;$ str = trim($ str);if($ cutSlashes)$ str = strip slashes($ str);if($ hashhtml){ $ str = preg _ replace(“/(\ & lt;【^\<】* \ & gt|\r|\n|\s|\【。+?\】)/是“,““,$ str);$ str = htmlspecialchars($ str);} else { $ str = htmlspecialchars($ str);} if($ cutLen & amp;& ampstrlen(str)& gt;$ cutLen){ $ nStr =“;if($ CFG _ soft _ lang = =‘utf-8′){ $ n = 0;$ TN = 0;$ NOC = 0;while($ n & lt;strlen($ str)){ $ t = ord($ str【$ n】);if($ t = = 9 | | $ t = = 10 | |(32 & lt;= $ t & amp& amp$ t & lt= 126)){ $ TN = 1;$ n++;$ noc++;} else if(194 & lt;= $ t & amp& amp$ t & lt= 223){ $ TN = 2;$ n+= 2;$ NOC+= 2;} else if(224 & lt;= $ t & amp& amp$ t & lt239){ $ TN = 3;$ n+= 3;$ NOC+= 2;} else if(240 & lt;= $ t & amp& amp$ t & lt= 247){ $ TN = 4;$ n+= 4;$ NOC+= 2;} else if(248 & lt;= $ t & amp& amp$ t & lt= 251){ $ TN = 5;$ n+= 5;$ NOC+= 2;} else if($ t = = 252 | | $ t = = 253){ $ TN = 6;$ n+= 6;$ NOC+= 2;} else { $ n++;} if($ NOC & gt;= $ cutLen)break;} if($ NOC & gt;$ cutLen)$ n-= $ TN;$ nStr = substr($ str,0,$ n);} else { for($ I = 0;$ i & lt$ cutLen–1;$ i++){ if(ord($ str【$ I】)& gt;127){ $ nStr。= $ str【$ I】。$ str【$ I+1】;$ i++;} else { $nStr。= $ str【$ I】;} } } $str = $nStr。$ oDot} if($ add slashes)$ str = add slashes($ str);$ str = htmlspecialchars _ decode($ str);return trim(str);}}第二,全站使用cn_substr()函数,不管你的程序是gbk还是utf8;

例如,如果要调用10个单词(混合拼音和汉字):【field:title function =‘cn _ substr(@ me,20)‘】。

这几篇文章你可能也喜欢:

本文由主机参考刊发,转载请注明:如何通过dedecms重新定义cn_substr函数来更准确的截取字数? https://zhujicankao.com/110554.html

【腾讯云】领8888元采购礼包,抢爆款云服务器 每月 9元起,个人开发者加享折上折!
打赏
转载请注明原文链接:主机参考 » 如何通过dedecms重新定义cn_substr函数来更准确的截取字数?
主机参考仅做资料收集,不对商家任何信息及交易做信用担保,购买前请注意风险,有交易纠纷请自行解决!请查阅:特别声明

评论 抢沙发

评论前必须登录!