preg_match(): Unknown modifier '/' [duplicate]

落花浮王杯 提交于 2021-02-05 12:21:52

问题


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

回答1:


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.



来源:https://stackoverflow.com/questions/56897007/preg-match-unknown-modifier

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!