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

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

WBOY
Release: 2016-06-13 12:53:53
Original
1197 people have browsed it

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


------解决方案--------------------
需要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);
Copy after login

sdfg哈哈sdfgfdgAShtt方法

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