字符串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;}

解决方案 »

  1.   

    那要看是utf8还是gb2312.
    我看你的是utf8的检测。utf8中文占3字节,头字节>127。
    str_split后,检测到大于127的,后面2个字节也获取即可。
      

  2.   

    $k1 = mb_substr($this->Keywords, 0, 1, "UTF-8");
      

  3.   

    谢谢LS,字符串是用户输入的,不确定是什么编码
    但我用的是DEDECMS GBK的,用户输入的字符串是否就是gb2312的呢?
      

  4.   

    mb_detect_encoding($str, "gbk, gb2312, utf-8")可以检测文字编码。不过经常会出错。还是建议用utf-8版的cms。
      

  5.   

    你可以用mb_detect_encoding去检测输入字符串的编码。
      

  6.   


    $str = '一?二?';
    $arr = preg_split('/\?|?| /', $str, -1, PREG_SPLIT_OFFSET_CAPTURE);
    print_r($arr);
    //得到二维数组,$arr[$i][0] 匹配满足的字符,$arr[$i][1]匹配位置
    //这个位置是strlen,如果中文算一个字符,只需要截取前面那段字符,用mb_strlen即可
      

  7.   

    编码涉及到中日文,就复杂了典型的,如果是Unicode编码“直”,中日编码是一样的“步”和“歩”,注意字型,编码是不一样的单单凭短短的几个字节,是区分不了中日文字的。
      

  8.   


    $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;
    }