用于荷兰语手机的正则表达式的PHP代码不起作用

时间:2014-04-14 12:30:10

标签: php regex

我实际上使用了Regular expression for Dutch phone number中给出的解决方案中的表达式,我的php代码如下,但是这段代码不起作用。

代码很简单,但我不知道哪里出错了?

define("REGEXP_PHONE_NL","(^\+[0-9]{2}|^\+[0-9]{2}\(0\)|^\(\+[0-9]{2}\)\(0\)|^00[0-9]{2}|^0)([0-9]{9}$|[0-9\-\s]{10}$)");

$string = "+31123456789"; //based on solution given in https://stackoverflow.com/questions/17949757/regular-expression-for-dutch-phone-number

echo(filter_var($string, FILTER_VALIDATE_REGEXP,array("options"=>array("regexp"=>REGEXP_PHONE_NL))));

2 个答案:

答案 0 :(得分:1)

正则表达式本身有效,但你忘了在模式的开头和结尾添加相同的字符(分隔符)。

<?php
    define("REGEXP_PHONE_NL","/(^\+[0-9]{2}|^\+[0-9]{2}\(0\)|^\(\+[0-9]{2}\)\(0\)|^00[0-9]{2}|^0)([0-9]{9}$|[0-9\-\s]{10}$)/");
    $string = "+316123456789"; //based on solution given in http://stackoverflow.com/questions/17949757/regular-expression-for-dutch-phone-number
    var_dump(filter_var($string, FILTER_VALIDATE_REGEXP,array("options"=>array("regexp"=>REGEXP_PHONE_NL))));

看到它正常工作here

答案 1 :(得分:0)

缺少在正则表达式模式中添加正则表达式字符正斜杠(/)我在前后添加正斜杠,

检查此 Demo code Viper

PHP

<?php
define("REGEXP_PHONE_NL","/(^\+[0-9]{2}|^\+[0-9]{2}\(0\)|^\(\+[0-9]{2}\)\(0\)|^00[0-9]{2}|^0)([0-9]{9}$|[0-9\-\s]{10}$)/");

$string = "+316123456789"; //based on solution given in http://stackoverflow.com/questions/17949757/regular-expression-for-dutch-phone-number

echo filter_var($string, FILTER_VALIDATE_REGEXP,array("options"=>array("regexp"=>REGEXP_PHONE_NL)));

?>

结果

+316123456789