0

When i give this regular expression for validating URL it shows error...

preg_match(): Unknown modifier '/' in C:\wamp64\www\php\Form Validation\form.php on line 36

if(!preg_match("/%^(?:(?:https?|ftp)://)(?:\S+(?::\S*)?@|\d{1,3}(?:\.\d{1,3}){3}|(?:(?:[a-z\d\x{00a1}-\x{ffff}]+-?)*[a-z\d\x{00a1}-\x{ffff}]+)(?:\.(?:[a-z\d\x{00a1}-\x{ffff}]+-?)*[a-z\d\x{00a1}-\x{ffff}]+)*(?:\.[a-z\x{00a1}-\x{ffff}]{2,6}))(?::\d+)?(?:[^\s]*)?$%iu/",$website)){
            $websiteerror="Invalid URL";
Dharman
  • 30,962
  • 25
  • 85
  • 135

1 Answers1

2

Since you used forward slash / as the delimiter, you must escape it inside the regex:

if (!preg_match("/%^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@|\d{1,3}(?:\.\d{1,3}){3}|(?:(?:[a-z\d\x{00a1}-\x{ffff}]+-?)*[a-z\d\x{00a1}-\x{ffff}]+)(?:\.(?:[a-z\d\x{00a1}-\x{ffff}]+-?)*[a-z\d\x{00a1}-\x{ffff}]+)*(?:\.[a-z\x{00a1}-\x{ffff}]{2,6}))(?::\d+)?(?:[^\s]*)?$%iu/",$website)) {
    $websiteerror = "Invalid URL";
}

Now the code runs for me, but I still get this warning:

PHP Warning: preg_match(): Compilation failed: character value in \x{} or \o{} is too large at offset 106 in source_file.php on line 3

It seems PHP doesn't completely like the hex ranges you used in some of your character classes.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360