Php 为什么不是';这个正则表达式不工作吗?

Php 为什么不是';这个正则表达式不工作吗?,php,html,regex,validation,Php,Html,Regex,Validation,我希望数字只能是数字,空格,减号,加号,没有其他,最好是最少5位,最多12位 当我尝试它时,会发生什么事情都没有通过,即:“12345”失败 有什么帮助吗 if(!preg_match('^[\+0-9\s\-]+$',$tel_nr)) { die("Invalid number."); } 您忘记使用分隔符。添加分隔符+{minlenght,maxlenght} !preg_match('/^[\+0-9\

我希望数字只能是数字,空格,减号,加号,没有其他,最好是最少5位,最多12位

当我尝试它时,会发生什么事情都没有通过,即:“12345”失败

有什么帮助吗

if(!preg_match('^[\+0-9\s\-]+$',$tel_nr))
            { 
                die("Invalid number.");
            } 

您忘记使用分隔符。

添加分隔符+{minlenght,maxlenght}

!preg_match('/^[\+0-9\s\-]{5,12}$/',$tel_nr))

必须使用/来分隔表达式的开始和结束。以下代码工作:

if(!preg_match('~^[\+0-9\s\-]{5,12}$~',$tel_nr))
            { 
                die("Invalid number.");
            } 

您是在寻找总共5-12个字符,还是5-12个数字,其他字符是可选的?如果是数字,则需要如下内容:

if (!preg_match('/^[\+0-9\s\-]+$/',$tel_nr)) { 
  die("Invalid number.");
}     

好的,能帮我把最大值和最小值加入到这里面吗:)?@Camran:问题似乎已经解决了!请随意接受答案。
if (!preg_match('~^(?:[\+\s\-]*[0-9]){5,12}[\+\s\-]*$~', $tel_nr))