Warning: preg_match() [function.preg-match]: No en

2020-04-08 11:22发布

I am trying to resolve an issue I have been having with one of my wordpress plugins.

This is line 666

function isUrl($url)
{
    return preg_match("^http:\/\/[-0-9a-z\._]+.*$/i", trim( $url ));
}

What are your inputs on how I can resolve this warning? It's quite irritating. I can't figure it out and I've tried, researched everything!

3条回答
来,给爷笑一个
2楼-- · 2020-04-08 11:55

As it has been said in other answers, you have to use delimiters arround the regex. You have also the possibility to use another character than / to avoid escaping them:

return preg_match("~^http://[-0-9a-z._]+.*$~i", trim( $url ));

And you could use \w that is shortcut of [a-zA-Z0-9_]:

return preg_match("~^http://[-\w.]+.*$~", trim( $url ));
查看更多
Luminary・发光体
3楼-- · 2020-04-08 12:01

/ is missing. preg_match('/pattern/',$string);

preg_match("/^http:\/\/[-0-9a-z\._]+.*$/i", trim( $url ));
查看更多
仙女界的扛把子
4楼-- · 2020-04-08 12:16

Your regex needs a delimiter around it, e.g. /

 return preg_match("/^http:\/\/[-0-9a-z\._]+.*$/i", trim( $url ));

This delimiter can be different characters but it must be the same before and after your regex. You could e.g. also do this

~regex~

or

#regex#

The advantage is, use a delimiter that you don't have inside the regex. If you use / as delimiter and want to match "/" you have to escape it inside the regex ///, if you change the delimiter you could do #/# to match a "/"

查看更多
登录 后发表回答