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

I am trying to solve a question that I have with one of my wordpress plugins.

This is line 666

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

What is your data on how I can resolve this warning? This is pretty annoying. I can’t figure it out, and I tried, explored everything!

+4
source share
3 answers

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

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

As said in other answers, you should use delimiters around the regex. You also have the option of using a different character than / to avoid escaping them:

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

And you can use \w , which is the shortcut [a-zA-Z0-9_] :

 return preg_match("~^http://[-\w.]+.*$~", trim( $url )); 
+2
source

Your regex needs a delimiter around it, for example. /

  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 regular expression. You can, for example, also do this.

 ~regex~ 

or

 #regex# 

The advantage is to use a delimiter that does not exist inside the regular expression. If you use / as a delimiter and want to match "/", you need to avoid it inside the regular expression ///, if you change the delimiter, you can do # / # to match "/"

+1
source

Source: https://habr.com/ru/post/1501192/


All Articles