字符串1:一?二? //中文的问号
字符串2:?一?二 //英文的问号
字符串3:一 二 //中间有空格php如何匹配出以上字符串里的中文文字?并求出该字符的位置
搞了半天搞不定,以下我的代码,大家别笑话 $k1 = "";
$k2 = "";
$k3 = "";
$k4 = "";
if(ord(substr($this->Keywords,1,1))>127)
{
$k1 = substr($this->Keywords,0,2);
}else {$k1=false;}
if(ord(substr($this->Keywords,2,1))>127)
{
$k2 = substr($this->Keywords,2,2);
}else {$k2=false;}
if(ord(substr($this->Keywords,3,1))>127)
{
$k3 = substr($this->Keywords,3,2);
}else {$k3=false;}
if(ord(substr($this->Keywords,4,1))>127)
{
$k4 = substr($this->Keywords,4,2);
}else {$k4=false;}
字符串2:?一?二 //英文的问号
字符串3:一 二 //中间有空格php如何匹配出以上字符串里的中文文字?并求出该字符的位置
搞了半天搞不定,以下我的代码,大家别笑话 $k1 = "";
$k2 = "";
$k3 = "";
$k4 = "";
if(ord(substr($this->Keywords,1,1))>127)
{
$k1 = substr($this->Keywords,0,2);
}else {$k1=false;}
if(ord(substr($this->Keywords,2,1))>127)
{
$k2 = substr($this->Keywords,2,2);
}else {$k2=false;}
if(ord(substr($this->Keywords,3,1))>127)
{
$k3 = substr($this->Keywords,3,2);
}else {$k3=false;}
if(ord(substr($this->Keywords,4,1))>127)
{
$k4 = substr($this->Keywords,4,2);
}else {$k4=false;}
我看你的是utf8的检测。utf8中文占3字节,头字节>127。
str_split后,检测到大于127的,后面2个字节也获取即可。
但我用的是DEDECMS GBK的,用户输入的字符串是否就是gb2312的呢?
$str = '一?二?';
$arr = preg_split('/\?|?| /', $str, -1, PREG_SPLIT_OFFSET_CAPTURE);
print_r($arr);
//得到二维数组,$arr[$i][0] 匹配满足的字符,$arr[$i][1]匹配位置
//这个位置是strlen,如果中文算一个字符,只需要截取前面那段字符,用mb_strlen即可
$str="规:范化 不得不知,道代动不,动不得不风格感?表 ";
$result=getCN($str);//UTF-8编码调用
//$result=getCN($str,"GB2312",2);//GB2312编码调用
//$result=getCN($str,"?",单个字符字节数);//?编码调用
print_r($result);
function getCN($string,$charset="UTF-8",$charnum=3){
$tmpstr = $string;
$utf8 = $charset=="UTF-8"?true:false;
!$utf8 && $string = iconv($charset,"UTF-8",$string);
preg_match_all("/[\x80-\xff]{3}/",$string,$data);
$cnarr=array();
foreach($data[0] as $key=>$char){
if(!preg_match_all("/[\\pP]/u",$char,$math)){
$cnarr[$key]['char']=$utf8?$char:iconv("UTF-8",$charset,$char);
$cnarr[$key]['start']=strpos($tmpstr,$cnarr[$key]['char']);
$cnarr[$key]['end']=$cnarr[$key]['start']+$charnum;
}
}
return $cnarr;
}