您现在的位置是:首页 > cms教程 > phpcms教程phpcms教程

phpcms-v9自带的字符串截取函数的方法

雷友水2025-06-09phpcms教程已有人查阅

导读1.phpcms-v9自带的字符串截取函数在phpcms/libs/functions/global.func.php文件中:不管是字母还是中文,都截取制定的长度

1.phpcms-v9自带的字符串截取函数在phpcms/libs/functions/global.func.php文件中:
/**
* 字符截取 支持UTF8/GBK
* @param $string
* @param $length
* @param $dot
*/
function str_cut($string, $length, $dot = '...') {
$strlen = strlen($string);
if($strlen <= $length) return $string;
$string = str_replace(array(' ',' ', '&', '"', ''', '“', '”', '-', '<', '>', '·', '…'), array('∵',' ', '&', '"', "'", '“', '”', '-', '<', '>', '·', '…'), $string);
$strcut = '';
if(strtolower(CHARSET) == 'utf-8') {
$length = intval($length-strlen($dot)-$length/3);
$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);
$strcut = str_replace(array('∵', '&', '"', "'", '“', '”', '-', '<', '>', '·', '…'), array(' ', '&', '"', ''', '“', '”', '-', '<', '>', '·', '…'), $strcut);
} else {
$dotlen = strlen($dot);
$maxi = $length - $dotlen - 1;
$current_str = '';
$search_arr = array('&',' ', '"', "'", '“', '”', '-', '<', '>', '·', '…','∵');
$replace_arr = array('&',' ', '"', ''', '“', '”', '-', '<', '>', '·', '…',' ');
$search_flip = array_flip($search_arr);
for ($i = 0; $i < $maxi; $i++) {
$current_str = ord($string[$i]) > 127 ? $string[$i].$string[++$i] : $string[$i];
if (in_array($current_str, $search_arr)) {
$key = $search_flip[$current_str];
$current_str = str_replace($search_arr[$key], $replace_arr[$key], $current_str);
}
$strcut .= $current_str;
}
}
return $strcut.$dot;
}
//weiyanhui自己添加,字符串截取函数,建议使用自己写的字符串截取函数,需要在php.ini中开启php_mbstring.dll扩展
function str_cut1($string, $length, $dot = '...') {
return mb_substr($string,0,$length,'utf-8').$dot;
}
echo str_cut1("abcdefg",3,"...");//结果:abc...
echo str_cut1("我们都是我国人",3,"...");//结果:我们都...
不管是字母还是中文,都截取制定的长度

本文标签:

很赞哦! ()

相关源码

  • 响应式WordPress简约博客主题Alt_BlogAlt_Blog主题该模板为博客内容展示设计,采用简约现代的风格理念,打造清晰的内容呈现平台。响应式布局确保在不同设备上都能获得良好的浏览体验,帮助博主更好地展示和分享内容。查看源码
  • (自适应)电子元件电路板元器件pbootcms网站源码下载为电子元器件、电路板制造类企业设计,特别适合展示产品参数、技术规格等内容。采用响应式技术,确保各类电子元件在不同设备上都能清晰展示。查看源码
  • (自适应)WordPress二次元博客主题SakurairoSakurairo主题为二次元内容创作者设计,提供丰富的动漫风格元素和个性化的展示效果。该主题在原有Sakura主题基础上进行了功能增强,支持多种自定义设置,满足动漫爱好者建立个人博客的需求。查看源码
  • (自适应)蓄电池能源智能数码科技产品pbootcms模板源码下载本款基于PbootCMS开发的网站模板为蓄电池及能源科技企业设计,特别适合锂电池、储能系统、新能源电池等产品的展示与推广。查看源码
  • (自适应响应式)个人作品技术文章博客网站模板下载基于PbootCMS内核开发的响应式博客模板,为数字营销、技术分享类内容打造。采用前沿设计理念,兼顾内容展示与阅读体验,适配各类终端设备。通过本模板可快速构建专业级行业博客,有效传播专业知识与案例成果。查看源码
  • (自适应)大气网络公司工作室个人作品展示网站模板免费下载基于PbootCMS内核开发的响应式网站模板,为网络技术服务类企业打造,具备高度可定制性。通过简洁现代的设计语言展现企业专业形象,后台数据实时同步机制确保多终端内容一致性,查看源码
分享笔记 (共有 篇笔记)
验证码:

本栏推荐