include dot(.) and hyphen(-) in a regex

后端 未结 4 1103
误落风尘
误落风尘 2021-01-29 12:56
 if(preg_match(\'/[^a-z\\-0-9]/i\', $value))

    {
    echo \"\";
}

how

相关标签:
4条回答
  • 2021-01-29 13:01

    Assuming you are talking about the regex, you escape both with a slash \.

    /[^a-z\.\-0-9]/i
    
    0 讨论(0)
  • 2021-01-29 13:06

    The hyphen does not need to be escaped if it is the last character in the Regex group. But the dot should always be escaped, so

    '/[^a-z\.0-9-]/i'
    

    Will check for a to z, . , 0 to 9 and - in that order, case insensitively.

    0 讨论(0)
  • 2021-01-29 13:10

    The hyphen doesn't need to be escaped if it's the first or last in the character class.
    The dot also doesn't need to escaped when used inside a character class [] i.e.:

    /[^a-z.0-9-]/i
    

    NOTES:

    Apart from the above, the meta characters that also need to be escaped inside a character class are:

    ^ (negation)
    - (range)
    ] (end of the class)
    \ (escape char)
    
    0 讨论(0)
  • 2021-01-29 13:18

    The hyphen is already in there, simply add an escaped dot using \..

    /edit: But as noted in the comment the escaping isn't needed.

    0 讨论(0)
提交回复
热议问题