Jump to content

Regular Expression Matching Issue


tomtimms

Recommended Posts

I have a form where a user types in their web address and I want to check the database to make sure it doesnt exists.  Here is my function.

 

    function valid_url_exist ($str)

    {

        return (preg_match('@^(?:https://\www.|http://\www.|http://|https://)?([^/]+)@i', $str, $matches));

    }

 

and here is my check

 

    if(valid_url_exist($url) === valid_url_exist($db_url) ) {

       

   

        $error .='This site already exists in our database';

       

    } 

 

I insert http://www.mysite.com and it takes (doesn't exist), however if I go and try and insert http://mysite.com it takes and the error check doesn't work.  It only works on exact matches so if i try http://www.mysite.com it tells me it exists.  Anyone have an idea?

 

Link to comment
https://forums.phpfreaks.com/topic/199500-regular-expression-matching-issue/
Share on other sites

Oh wow. That regexp is a bit awkward, but you need to escape those dots.

#^https?://(www\.)?[^/]+#i

 

I'm not sure how useful this line is:

if(valid_url_exist($url) === valid_url_exist($db_url) ) {

 

You're checking if a valid url matches another valid url? So as long as both are valid urls, it's good? Why not just do this then?

if(valid_url_exist($url) && valid_url_exist($db_url) ) {

Just a word of caution ... you can't necessarily assume that all those URL variations point to the same site. Although it is often the case, all of the following could be serving different sites:

 

 

-D

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.