<?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个字符) ?>
PHP字符操作之限制新闻标题的显示字数
最新推荐文章于 2021-04-02 08:14:41 发布