PHP字符操作之限制新闻标题的显示字数
<?php header("Content-Type:text/html;charset=GB2312"); function cutstr($str,$cutleng) { $str = $str; //要截取的字符串 $cutleng = $cutleng; //要截取的长度 $strleng = strlen($str); //字符串长度 if($cutleng>$strleng)return $str;//字符串长度小于规定字数时,返回字符串本身 $notchinanum = 0; //初始不是汉字的字符数 for($i=0;$i<$cutleng;$i++) { if(ord(substr($str,$i,1))<=128) { $notchinanum++; } } if(($cutleng%2==1)&&($notchinanum%2==0)) //如果要截取奇数个字符,所要截取长度范围内的字符必须含奇数个非汉字,否则截取的长度加一 { $cutleng++; } if(($cutleng%2==0)&&($notchinanum%2==1)) //如果要截取偶数个字符,所要截取长度范围内的字符必须含偶数个非汉字,否则截取的长度加一 { $cutleng++; } return substr($str,0,$cutleng); } ?> <?php $row_Rec_news["news_title"]="dddddddd截取指定数量的字符串显示eefffe"; echo cutstr( $row_Rec_news["news_title"],20);echo ("...");//从后台读出新闻标题的前10个汉字(20个字符) ?>
声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
- 上一篇:没有了
- 下一篇: php JSON数据格式化方法 .