Warning: preg_match() [function.preg-match]: No ending delimiter '^' found

眉间皱痕 提交于 2019-12-04 03:50:01

问题


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!


回答1:


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

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



回答2:


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



回答3:


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



来源:https://stackoverflow.com/questions/18686871/warning-preg-match-function-preg-match-no-ending-delimiter-found

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