带有preg_match()的PHP正则表达式
问题描述:
I want to check if a variable contains only alphabetical characters and apostrophes (') and dashes (-) and white space ( ) characters. How can do I this with preg_match()
in PHP?
I have /[^a-zA-Z'-\s]/i and it solved without apostroph.
我想检查变量是否只包含字母字符和撇号(')和破折号( - )以及空格 ()字符。 我怎么能用PHP中的 我有/ [^ a-zA-Z' - \ s] / i它解决了 没有叛逆者。 p>
div> preg_match() code>呢? p>
答
preg_match('/^[\s\pL'-]+$/',$string)
This is the way I'd do it
//EDIT
Maybe if you have a minimum count of letters, use this:
preg_match("/^[\s\pL'-]{5,}$/",$string) //{x,} ---> x is your min number
答
Try /^[a-zA-Z'\-\s]*$/
as your regex string. Note that this regex will also match blank strings.