简体   繁体   English

正则表达式阻止特殊字符

[英]Regex blocking special characters

I'm using PHP Version 5.3.27 我正在使用PHP版本5.3.27

I'm trying to get my regex to match whitespace, and special characters such as ♦◘•♠♥☻, the other known special characters which are %$#&*@ are already matched, but somehow the ones I mentioned before are not matched.. 我试图让我的正则表达式匹配空白,并且特殊字符如♦◘•♠♥☻,其他已知的特殊字符%$#&* @已经匹配,但不知怎的,我之前提到的那些不是匹配..

Current regex 目前的正则表达式

preg_match('/^[a-zA-Z0-9[:space:]]+$/', $login)

My apology for asking two questions on the same subject. 我就同一主题提出两个问题表示道歉。 I hope this one is clear enough for you. 我希望这个对你来说足够清楚。

Your regex doesn't contain any reference to the special characters mentioned. 您的正则表达式不包含对提到的特殊字符的任何引用。 You would need to include them in the character class for them to be matched. 您需要将它们包含在角色类中以便匹配它们。

To match those kinds of special characters you can use the unicode values. 要匹配这些特殊字符,您可以使用unicode值。

Example: 例:

\u0000-\uFFFF
\x00-\xFF

The top is UTF-16, the bottom is UTF-8. 顶部是UTF-16,底部是UTF-8。

Refer to a UTF-8/16 character table online to match up your symbols with their unicode values, then create a range to keep your expression short. 在线参考UTF-8/16字符表,将符号与其unicode值匹配,然后创建一个范围以保持表达式简短。

use this 用这个

[\W]+ 

will match any non-word character. 将匹配任何非单词字符。

You can use the \\p{S} character class (or \\p{So} ) that matches symbol characters (that includes this kind of characters: ╭₠☪♛♣♞♉♆☯♫) : 您可以使用匹配符号字符的\\p{S}字符类(或\\p{So}(包括这种字符:╭₠☪♛♣♫♫)

preg_match('/^[a-zA-Z0-9\h\p{S}]+$/u', $login)

To find more possibilities you can check the pcre documentation at: http://www.pcre.org/pcre.txt 要查找更多可能性,您可以访问以下网址查看pcre文档: http ://www.pcre.org/pcre.txt

If you need to be more precise, the best way is to use character ranges in the character class. 如果需要更精确,最好的方法是在字符类中使用字符范围。 You can find code of characters here . 你可以在这里找到字符代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM