php中如何对这样的字符串进行切分? $str='/闑鮹/login/..'echo current(str_split($str,4)) 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 /闑鮹/login/.. 前4个是 /字字/要得到的是这两个字吧?用 / 分割字符串就得到了 嘿嘿 如果位置不固定 正则来的效率高也简单 先ord判断一下就可以了。如果大于127就切两个。没有简单的方法,如果装了mb_string,可以. 用ord不行,我使用的unicode编码,ord判断ASCII码 function substr_cut($str_cut,$length = 2) { if (strlen($str_cut) > $length){ for($i=0; $i < $length; $i++) if (ord($str_cut[$i]) > 128) $i++; $str_cut = substr($str_cut,0,$i); } return $str_cut; }$str_cut = '判o&r#d断一下就可以了';$cc = substr_cut($str_cut,'4');echo $cc; 不好意识没看清楚,你要切/闑鮹/login/..得先用正则提取出\&+\#+\d{5};把这些提取出来判断一下位数再取后面的字符长度。 <?phppreg_match("/(&#[0-9]+;|[a-z]+)(?1){3}/iU","闑鮹login",$match);print_r($match); // ["闑鮹lo","闑"]/*** 你的意思没有表达得很清楚* 直接说 源字符串是什么 要的结果是什么样子就行* 还有一些特殊情况也要说明 比如那些字符串建有没其他字符 比如斜杠 数字什么的。* 你说的含糊的 我都不知到你那里面的斜杠是字符串还是你用来说明截取点的。*/?>参考:http://topic.csdn.net/u/20080331/14/012e0988-5a54-4831-b88b-7c34d4a4f522.htmlhttp://topic.csdn.net/u/20080411/17/a978257c-ae4b-4247-ad0b-78f424ff7597.html 这个函数约绝对可以用,我一直在用,中文占两个长度,你可以试试,中英文通用,函数中对中英文进行了判断处理了function utf_substr($str,$len){for($i=0;$i<$len;$i++) {$temp_str=substr($str,0,1);if(ord($temp_str) > 127) //截取字符串第一字符的ascii码是否大于127,0-127的128个ASCII码代表了字母数字即一些标点符号等,超过此范围则为需要以unicode码显示{$i++; if($i<$len){$new_str[]=substr($str,0,3);$str=substr($str,3);}}else{$new_str[]=substr($str,0,1);$str=substr($str,1);}}return join($new_str);} unicode不能用ord判断,所以16楼的还是没有办法用啊 <?phppreg_match_all("/(&#[0-9]+;|[a-z]+)/iU","/闑鮹/login/",$match);$result = array_slice($match[1],0,4);$result = implode('',$result);echo $result;// String "闑鮹lo"?> 第一步:原码是html实体,可以先还原成html非实体,这样中文可以正常显示了第二步:中英文分离这个就多了,得看你的中英文是如何混排的,想要分成什么样的!可以字符串截取,也可以用正则用正则:英文就是26个字母构成的,中文则不是 function FSubstr($title,$start,$len="",$magic=true) {/** * powered by Smartpig * mailto:[email protected] */ $length = 0;if($len == "") $len = strlen($title);//判断起始为不正确位置if($start > 0){ $cnum = 0; for($i=0;$i<$start;$i++) { if(ord(substr($title,$i,1)) >= 128) $cnum ++; } if($cnum%2 != 0) $start--; unset($cnum);}if(strlen($title)<=$len) return substr($title,$start,$len);$alen = 0;$blen = 0;$realnum = 0;for($i=$start;$i<strlen($title);$i++){ $ctype = 0; $cstep = 0; $cur = substr($title,$i,1); if($cur == "&") { if(substr($title,$i,4) == "<") { $cstep = 4; $length += 4; $i += 3; $realnum ++; if($magic) { $alen ++; } } else if(substr($title,$i,4) == ">") { $cstep = 4; $length += 4; $i += 3; $realnum ++; if($magic) { $alen ++; } } else if(substr($title,$i,5) == "&") { $cstep = 5; $length += 5; $i += 4; $realnum ++; if($magic) { $alen ++; } } else if(substr($title,$i,6) == """) { $cstep = 6; $length += 6; $i += 5; $realnum ++; if($magic) { $alen ++; } } else if(substr($title,$i,6) == "'") { $cstep = 6; $length += 6; $i += 5; $realnum ++; if($magic) { $alen ++; } } else if(preg_match("/&#(\d+);/i",substr($title,$i,8),$match)) { $cstep = strlen($match[0]); $length += strlen($match[0]); $i += strlen($match[0])-1; $realnum ++; if($magic) { $blen ++; $ctype = 1; } } }else{ if(ord($cur)>=128) { $cstep = 2; $length += 2; $i += 1; $realnum ++; if($magic) { $blen ++; $ctype = 1; } }else{ $cstep = 1; $length +=1; $realnum ++; if($magic) { $alen++; } } } if($magic) { if(($blen*2+$alen) == ($len*2)) break; if(($blen*2+$alen) == ($len*2+1)) { if($ctype == 1) { $length -= $cstep; break; }else{ break; } } }else{ if($realnum == $len) break; }}unset($cur);unset($alen);unset($blen);unset($realnum);unset($ctype);unset($cstep);return substr($title,$start,$length);} http://bbs.lamp.org.cn <?phpprint m_substr("ss还是dd",1,3);/*现写的,大概是酱紫*/function m_substr($str){ preg_match_all("/[\x80-\xff]?./",$str,$arr); if(func_num_args() == 2) { $len1 = func_get_arg(1); return implode("",array_slice($arr[0],$len1)); } if(func_num_args() >= 3) { $len1 = func_get_arg(1); $len2 = func_get_arg(2); return implode("",array_slice($arr[0],$len1,$len2)); } }?> 【求助】PHP COM ADODB 拒绝访问 急求 nl2br()函数的使用错误 急需 此迅雷的 CSS 效果 ..在线等...马上放分.! PHP的域名查询类(不要小偷程序!谢谢!) 在做PHP查询数据库时--- 有办法在传输XML数据流的时候压缩传输吗? 在哪里找javasript写的函数? 求救一个php的附件邮件发送程序! 怎么从mysql_fetch_array()中取得数组?? 在REDHAT 8下配置MYSQL时出现的问题. 求PHP执行存储过程,返回多个记录集中.怎么再把值 print出来 怎样在数据库中返回多行数据?
前4个是 /字字/
要得到的是这两个字吧?用 / 分割字符串就得到了 嘿嘿 如果位置不固定 正则来的效率高也简单
没有简单的方法,如果装了mb_string,可以.
if (strlen($str_cut) > $length){
for($i=0; $i < $length; $i++)
if (ord($str_cut[$i]) > 128) $i++;
$str_cut = substr($str_cut,0,$i);
}
return $str_cut;
}$str_cut = '判o&r#d断一下就可以了';
$cc = substr_cut($str_cut,'4');
echo $cc;
<?php
preg_match("/(&#[0-9]+;|[a-z]+)(?1){3}/iU","闑鮹login",$match);
print_r($match); // ["闑鮹lo","闑"]
/**
* 你的意思没有表达得很清楚
* 直接说 源字符串是什么 要的结果是什么样子就行
* 还有一些特殊情况也要说明 比如那些字符串建有没其他字符 比如斜杠 数字什么的。
* 你说的含糊的 我都不知到你那里面的斜杠是字符串还是你用来说明截取点的。
*/?>
参考:
http://topic.csdn.net/u/20080331/14/012e0988-5a54-4831-b88b-7c34d4a4f522.htmlhttp://topic.csdn.net/u/20080411/17/a978257c-ae4b-4247-ad0b-78f424ff7597.html
{
for($i=0;$i<$len;$i++)
{
$temp_str=substr($str,0,1);
if(ord($temp_str) > 127)
//截取字符串第一字符的ascii码是否大于127,0-127的128个ASCII码代表了字母数字即一些标点符号等,超过此范围则为需要以unicode码显示
{
$i++;
if($i<$len)
{
$new_str[]=substr($str,0,3);
$str=substr($str,3);
}
}
else
{
$new_str[]=substr($str,0,1);
$str=substr($str,1);
}
}
return join($new_str);
}
<?php
preg_match_all("/(&#[0-9]+;|[a-z]+)/iU","/闑鮹/login/",$match);
$result = array_slice($match[1],0,4);
$result = implode('',$result);
echo $result;
// String "闑鮹lo"
?>
原码是html实体,可以先还原成html非实体,这样中文可以正常显示了第二步:中英文分离这个就多了,得看你的中英文是如何混排的,想要分成什么样的!可以字符串截取,也可以用正则用正则:英文就是26个字母构成的,中文则不是
{
/**
* powered by Smartpig
* mailto:[email protected]
*/
$length = 0;
if($len == "") $len = strlen($title);//判断起始为不正确位置
if($start > 0)
{
$cnum = 0;
for($i=0;$i<$start;$i++)
{
if(ord(substr($title,$i,1)) >= 128) $cnum ++;
}
if($cnum%2 != 0) $start--;
unset($cnum);
}if(strlen($title)<=$len) return substr($title,$start,$len);$alen = 0;
$blen = 0;$realnum = 0;for($i=$start;$i<strlen($title);$i++)
{
$ctype = 0;
$cstep = 0;
$cur = substr($title,$i,1);
if($cur == "&")
{
if(substr($title,$i,4) == "<")
{
$cstep = 4;
$length += 4;
$i += 3;
$realnum ++;
if($magic)
{
$alen ++;
}
}
else if(substr($title,$i,4) == ">")
{
$cstep = 4;
$length += 4;
$i += 3;
$realnum ++;
if($magic)
{
$alen ++;
}
}
else if(substr($title,$i,5) == "&")
{
$cstep = 5;
$length += 5;
$i += 4;
$realnum ++;
if($magic)
{
$alen ++;
}
}
else if(substr($title,$i,6) == """)
{
$cstep = 6;
$length += 6;
$i += 5;
$realnum ++;
if($magic)
{
$alen ++;
}
}
else if(substr($title,$i,6) == "'")
{
$cstep = 6;
$length += 6;
$i += 5;
$realnum ++;
if($magic)
{
$alen ++;
}
}
else if(preg_match("/&#(\d+);/i",substr($title,$i,8),$match))
{
$cstep = strlen($match[0]);
$length += strlen($match[0]);
$i += strlen($match[0])-1;
$realnum ++;
if($magic)
{
$blen ++;
$ctype = 1;
}
}
}else{
if(ord($cur)>=128)
{
$cstep = 2;
$length += 2;
$i += 1;
$realnum ++;
if($magic)
{
$blen ++;
$ctype = 1;
}
}else{
$cstep = 1;
$length +=1;
$realnum ++;
if($magic)
{
$alen++;
}
}
}
if($magic)
{
if(($blen*2+$alen) == ($len*2)) break;
if(($blen*2+$alen) == ($len*2+1))
{
if($ctype == 1)
{
$length -= $cstep;
break;
}else{
break;
}
}
}else{
if($realnum == $len) break;
}
}unset($cur);
unset($alen);
unset($blen);
unset($realnum);
unset($ctype);
unset($cstep);return substr($title,$start,$length);
}
<?phpprint m_substr("ss还是dd",1,3);
/*
现写的,大概是酱紫
*/
function m_substr($str)
{
preg_match_all("/[\x80-\xff]?./",$str,$arr);
if(func_num_args() == 2)
{
$len1 = func_get_arg(1);
return implode("",array_slice($arr[0],$len1));
}
if(func_num_args() >= 3)
{
$len1 = func_get_arg(1);
$len2 = func_get_arg(2);
return implode("",array_slice($arr[0],$len1,$len2));
}
}
?>