php中英文php 截取字符不同长度问题的探讨

扫扫二维码,随身浏览文档
手机或平板扫扫即可继续访问
php截取中英文混合字符串防止乱码
举报该文档含有违规或不良信息。
反馈该文档无法正常浏览。
举报该文档为重复文档。
推荐理由:
将文档分享至:
分享完整地址
文档地址:
粘贴到BBS或博客
flash地址:
支持嵌入FLASH地址的网站使用
html代码:
&embed src='/DocinViewer-4.swf' width='100%' height='600' type=application/x-shockwave-flash ALLOWFULLSCREEN='true' ALLOWSCRIPTACCESS='always'&&/embed&
450px*300px480px*400px650px*490px
支持嵌入HTML代码的网站使用
您的内容已经提交成功
您所提交的内容需要审核后才能发布,请您等待!
3秒自动关闭窗口在很多网页展示中,标题的长度有时需要量身定制,比如已经排定好的样式,一般都不会满足太长的标题,这就需要对标题进行截取。考虑到一般都是中文、英文、数字、英文符号、中文符号混合的标题,现有的函数只能使用mb_substr这样的针对编码的截取方式。但是这样的结果总是不令人满意的。
比如,文章标题有以下几种:
1、全英文:Celine Dion My Love Ultimate Essential Collection My Heart Will Go On
2、全中文:一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十
3、中英文混排(包含中英标点):一二三四五;&;六七八九十;&;'Celine Dion My Love
这里使用mb_substr截取10字符将是什么效果?
Celine Dio
一二三四五六七八九十
一二三四五;&;六七
这种残次不齐的效果在网页中出现,迟早会被老板骂死~~~
针对这种定制样式截取,一般都是需要规定截取后字符的宽度像素的,也就是说,截取后的宽度一般等长的。研究一番发现一般英文两个字符相当于汉字一个字的占位空间。所以写下如下函数,解决这个问题。
function mb_sub($str,$len,$encode='utf8',$start=0){
if($encode!='utf8'){
$str = mb_convert_encoding($str,'utf8',$encode);
$osLen = mb_strlen($str);
if($osLen&=$len){
return $str;
$string = mb_substr($str,$start,$len,'utf8');
$sLen = mb_strlen($string,'utf8');
$bLen = strlen($string);
$sCharCount = (3*$sLen-$bLen)/2;
if($osLen&=$sCharCount+$len){
$arr = preg_split('/(?&!^)(?!$)/u',mb_substr($str,$len+1,$osLen,'utf8'));//将中英混合字符串分割成数组(UTF8下有效)
$arr = preg_split('/(?&!^)(?!$)/u',mb_substr($str,$len+1,$sCharCount,'utf8'));
foreach($arr as $value){
if(ord($value)&128 && ord($value)&0){
$sCharCount = $sCharCount-1;
$sCharCount = $sCharCount-2;
if($sCharCount&=0){
$string.=$value;
return $string;
echo '一二三四五六七八九十一二三四五六七八九十一二三四五六七八九十',"\n";
$str = '不是病猫!苹果Mountain Lion系统4天下载量破300万次';
echo mb_sub($str,17);
&函数中,为了适应多种编码,将编码均转换为utf8,演示效果如下
阅读(...) 评论()php 截取一定长度的字符串(该函数对GB2312使用有效
来源:&&&时间: 19:02:13&&&阅读数:
[导读] php教程 截取一定长度的字符串(该函数对gb2312使用有效function ssubstr($string, $length, $dot =
) {global $ if(strlen($string)
截取一定长度的字符串(该函数对gb2312使用有效
function ssubstr($string, $length, $dot = ' ...') {
& if(strlen($string) &= $length) {
&& return $
& $string = str_replace(array('&', '&', '&', '&'), array('&', '&', '&', '&'), $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 {
&&&& if($noc &= $length)
&&& if($noc & $length)
&&& $n -= $
&& $strcut = substr($string, 0, $n);
& } else {
&&& for($i = 0; $i & $ $i++)
&&&& $strcut .= ord($string[$i]) & 127 ? $string[$i].$string[++$i] : $string[$i];
&$strcut = str_replace(array('&', '&', '&', '&'), array('&', '&', '&', '&'), $strcut);
&return $strcut.$
截取一定长度的字符串(该函数对gb2312使用有效)
function wordscut($string, $length ,$sss=0) {
if(strlen($string) & $length) {
&&&&&&&&&&&&&& if($sss){
$length=$length - 3;
$addstr=@# ...@#;
& for($i = 0; $i & $ $i++) {
&& if(ord($string[$i]) & 127) {
$wordscut .= $string[$i].$string[$i + 1]; //
&& } else {
$wordscut .= $string[$i];
& return $wordscut.$
除非特别声明,PHP100新闻均为原创或投稿报道,转载请注明作者及原文链接原文地址:
延伸阅读 More
haowubai&&
haowubai&&
haowubai&&
haowubai&&
haowubai&&
视频教程 Video
网站服务:
专题合作 : liuyh@
会员问题 : QQ
友情链接 : QQ
网站投稿 :
@php100官方
php100官方微信
Copyright (C) 2007-, All Rights Reserved 版权所有 上海创恩信息技术有限公司. 沪ICP备号

我要回帖

更多关于 php 截取字符 的文章

 

随机推荐