Home > Backend Development > PHP Tutorial > 正则从汉字字符串中取某一个汉字

正则从汉字字符串中取某一个汉字

WBOY
Release: 2016-06-23 13:59:54
Original
1078 people have browsed it

<?phpheader("Content-type: text/html; charset=gb2312"); $regex = '/大(\xhh{1})好啊/';   //这里该如何去写,我不会正则,希望大家能帮我解答一下,谢谢$str = '大家好啊';$matches = array(); if(preg_match($regex, $str, $matches)){    var_dump($matches);}?>
Copy after login


我想取出“家”字


回复讨论(解决方案)

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

header("Content-type: text/html; charset=utf-8");$regex = '/大(.*?)好啊/';$str = '大家好啊';$matches = array();  if(preg_match($regex, $str, $matches)){    var_dump($matches);//array(2) { [0]=> string(12) "大家好啊" [1]=> string(3) "家" }}
Copy after login
Copy after login
如果我str里面还有字母,得用gb2312格式的该如何去写?

有什么区别吗?

$regex = '/大(.*?)好啊/';$str = '大家好啊';$matches = array();   if(preg_match($regex, $str, $matches)){    var_dump($matches);}
Copy after login
Copy after login
array(2) { [0]=> string(8) "大家好啊" [1]=> string(2) "家" }

有什么区别吗?

$regex = '/大(.*?)好啊/';$str = '大家好啊';$matches = array();   if(preg_match($regex, $str, $matches)){    var_dump($matches);}
Copy after login
Copy after login
array(2) { [0]=> string(8) "大家好啊" [1]=> string(2) "家" } 会乱码。$str = 'a大家好啊';$regex = '/a大(.*?)好啊/';

会乱码。$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) "家" }}
Copy after login

怎么可能?

$regex = '/a大(.*?)好啊/';$str = 'a大家好啊';$matches = array();    if(preg_match($regex, $str, $matches)){    var_dump($matches);}
Copy after login
array(2) { [0]=> string(9) "a大家好啊" [1]=> string(2) "家" } 

文件编码? ??    

谢谢回答问题各位大神,版主大人可能没看到我问题要求的编码是gb2312的。尤文球迷给的答案我试过了可以的,编码问题总是令人蛋疼。

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template