Home  >  Article  >  Backend Development  >  正则从汉字字符串中取某一个汉字解决方案

正则从汉字字符串中取某一个汉字解决方案

WBOY
WBOYOriginal
2016-06-13 11:53:081034browse

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

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


我想取出“家”字
------解决方案--------------------

引用:
会乱码。$str = 'a大家好啊';$regex = '/a大(.*?)好啊/';


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