Home  >  Article  >  Backend Development  >  求正则表达式:只保留字符串中的汉字跟字母

求正则表达式:只保留字符串中的汉字跟字母

WBOY
WBOYOriginal
2016-06-13 12:53:531135browse

求正则表达式:只保留字符串中的汉字和字母
求正则表达式:只保留字符串中的汉字和字母


------解决方案--------------------
需要unicode
preg_replace('#[^a-z\u4e00-\u9fbb]#i', '', $string)

GBK下分开全角符号和汉字要复杂些
------解决方案--------------------
不是 utf-8 编码时,需转码
$s = '#$%^4sdfg3哈哈sdfgfdg$@#%@#$%AS<h>?><,.~!tt方法';<br />
echo preg_replace('/[\d\W_]/u', '', $s);

sdfg哈哈sdfgfdgAShtt方法

你#1的补充与原帖的需求不符
echo preg_replace('/[\d\W_a-z]/iu', '', $s);
哈哈方法
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