Joppo
Joppo

Reputation: 729

PHP code with regexp for dutch phone are not working

I actually used the expression from the solution given in Regular expression for Dutch phone number for my php code below, but this code is not working.

The code is simple but I don't see where I go wrong ?

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))));

Upvotes: 0

Views: 466

Answers (2)

Jaykumar Patel
Jaykumar Patel

Reputation: 27614

You are missing to put regexp identifier character forward slash (/) in your regular expression pattern i add forward slash before and after,

Check this 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)));

?>

Result

+316123456789

Upvotes: 0

sunshinejr
sunshinejr

Reputation: 4854

Regex itself works, but you forgot to put same characters in the beginning and in the end of the pattern (delimeters).

<?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))));

See it working here.

Upvotes: 1

Related Questions