PHP preg_match - only allow alphanumeric strings and - _ characters

后端 未结 5 2033
醉话见心
醉话见心 2020-12-04 15:50

I need the regex to check if a string only contains numbers, letters, hyphens or underscore

$string1 = \"This is a string*\";
$string2 = \"this_is-a-string\"         


        
相关标签:
5条回答
  • 2020-12-04 16:12

    Code:

    if(preg_match('/[^a-z_\-0-9]/i', $string))
    {
      echo "not valid string";
    }
    

    Explanation:

    • [] => character class definition
    • ^ => negate the class
    • a-z => chars from 'a' to 'z'
    • _ => underscore
    • - => hyphen '-' (You need to escape it)
    • 0-9 => numbers (from zero to nine)

    The 'i' modifier at the end of the regex is for 'case-insensitive' if you don't put that you will need to add the upper case characters in the code before by doing A-Z

    0 讨论(0)
  • 2020-12-04 16:12

    \w\- is probably the best but here just another alternative
    Use [:alnum:]

    if(!preg_match("/[^[:alnum:]\-_]/",$str)) echo "valid";
    

    demo1 | demo2

    0 讨论(0)
  • 2020-12-04 16:13

    Here is one equivalent of the accepted answer for the UTF-8 world.

    if (!preg_match('/^[\p{L}\p{N}_-]+$/u', $string)){
      //Disallowed Character In $string
    }
    

    Explanation:

    • [] => character class definition
    • p{L} => matches any kind of letter character from any language
    • p{N} => matches any kind of numeric character
    • _- => matches underscore and hyphen
    • + => Quantifier — Matches between one to unlimited times (greedy)
    • /u => Unicode modifier. Pattern strings are treated as UTF-16. Also causes escape sequences to match unicode characters

    Note, that if the hyphen is the last character in the class definition it does not need to be escaped. If the dash appears elsewhere in the class definition it needs to be escaped, as it will be seen as a range character rather then a hyphen.

    0 讨论(0)
  • 2020-12-04 16:20

    Why to use regex? PHP has some built in functionality to do that

    <?php
        $valid_symbols = array('-', '_');
        $string1 = "This is a string*";
        $string2 = "this_is-a-string";
    
        if(preg_match('/\s/',$string1) || !ctype_alnum(str_replace($valid_symbols, '', $string1))) {
            echo "String 1 not acceptable acceptable";
        }
    ?>
    

    preg_match('/\s/',$username) will check for blank space

    !ctype_alnum(str_replace($valid_symbols, '', $string1)) will check for valid_symbols

    0 讨论(0)
  • 2020-12-04 16:31
    if(!preg_match('/^[\w-]+$/', $string1)) {
       echo "String 1 not acceptable acceptable";
       // String2 acceptable
    }
    
    0 讨论(0)
提交回复
热议问题