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

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

This is line 666

Read More
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!

Related posts

Leave a Reply

3 comments

  1. 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 ));
    
  2. 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 “/”