日期:2014-05-16  浏览次数:20473 次

正则从汉字字符串中取某一个汉字
<?php
header("Content-type: text/html; charset=gb2312"); 
$regex = '/大(\xhh{1})好啊/';   //这里该如何去写,我不会正则,希望大家能帮我解答一下,谢谢
$str = '大家好啊';
$matches = array();
 
if(preg_match($regex, $str, $matches)){
    var_dump($matches);
}
?>


我想取出“家”字
------解决方案--------------------
引用:
会乱码。$str = 'a大家好啊';$regex = '/a大(.*?)好啊/';


header("Content-type: text/html; charset=gb2312"); 
$regex = '/大(.*?)好啊/';
$str = '大家好啊';
$str= mb_convert_encoding($str,'gb2312','UTF-8');
$regex = mb_convert_encoding($regex,'gb2312','UTF-8');;
$matches = array();
   
if(preg_match($regex, $str, $matches)){
    var_dump($matches);
//array(2) { [0]=> string(12) "大家好啊" [1]=> string(3) "家" }
}