doulaopu2343
2017-09-29 15:54
浏览 20
已采纳

preg_match,不包括逗号和点

if (!preg_match('/[^a-z ćčžđš\0-9]/i', $_POST['a'])) {
    echo 'error';
}

I use this for allowing only alphanumeric characters plus specific local characters for croatian language.

It works but it also allows comma, point... and maybe something else. How to exclude any other characters except a-z, my local characters, spaces and numbers?

for example - should not be allowed:

  • abc,
  • abc.
  • abc+

... and should be allowed:

  • abc

图片转代码服务由CSDN问答提供 功能建议

  if(!preg_match('/ [^azćčžđš\ 0-9] / i',$  _POST ['a'])){
 echo'error'; 
} 
   
 
 

我使用它只允许字母数字字符加上克罗地亚语的特定本地字符 语言。

它可以工作,但它也允许使用逗号,点...以及其他东西。 如何排除除az,我的本地字符,空格和数字之外的任何其他字符?

例如 - 不应该被允许:

  • abc,
  • abc。
  • abc +

    ... 并且应该被允许:

    • abc
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • donglian3061 2017-09-29 16:15
    已采纳

    There are 3 problems in your code:

    1) To check if your string contains forbidden characters and display the error message, you use a double negation:

    if (!preg_match('/[^allowed characters]/i', ...
    #   ^--------------^
    

    This means "if the string doesn't contain a forbidden character, then display 'error'".

    In my opinion the correct algorithm is more:

    if ( preg_match('/[^allowed characters]/i', ...
    

    2) If you escape the 0 in 0-9 inside a character class, you define a range between the null character and the character 9 (see the ascii table)

    3) You are dealing with unicode characters, you have to use the u modifier, otherwise the regex engine will read your string byte by byte and return false positive:

    if ( preg_match('/[^a-z ćčžđš0-9]/iu', $_POST['a']) )
        echo 'error!';
    

    To finish, accented characters can be written in two ways in unicode for example č can be:

    • the single codepoint U+010D (LATIN SMALL LETTER C WITH CARON)
    • the combination of the codepoint U+0063 (LATIN SMALL LETTER C) and the codepoint U+030C (COMBINING CARON)

    Your pattern will not handle the second case. To avoid the problem, you have to normalize your string first with the intl normalizer.

    点赞 评论
  • douchun9719 2017-09-29 15:58

    You have to remove the backslash before "0-9" in your regexp.
    You have double negation, and you can add the "^" for start and "$" for end. The Unicode chars need "u" modifier

    I have change your code like this :

    if (preg_match('/^[a-z ćčžđš0-9]*$/iu', $_POST['a'])) {
       echo 'ok';
    } else {
       echo 'error';
    }
    

    I suggest you to test your regexp with a online regex tester (for example https://regex101.com/)

    点赞 评论
  • dongtong1226 2017-09-29 16:01

    Having the backslash before 0-9 makes it a character group in the range between character 0 and number 9 (index 0 and 57, respectively), not number 0 and number 9. A comma (44) and a point (46) are coincidentally inside that range, so it doesn't work as you need it to.

    To make it work, remove the slash:

    [^a-z ćčžđš0-9]
    
    点赞 评论

相关推荐 更多相似问题