tomtimms Posted April 23, 2010 Share Posted April 23, 2010 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 More sharing options...
Ken2k7 Posted April 23, 2010 Share Posted April 23, 2010 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) ) { Link to comment https://forums.phpfreaks.com/topic/199500-regular-expression-matching-issue/#findComment-1047051 Share on other sites More sharing options...
tomtimms Posted April 23, 2010 Author Share Posted April 23, 2010 Yeah I am checking to see if the URL provided matches the URL in the database. Link to comment https://forums.phpfreaks.com/topic/199500-regular-expression-matching-issue/#findComment-1047054 Share on other sites More sharing options...
Ken2k7 Posted April 23, 2010 Share Posted April 23, 2010 Uh no. - preg_match. preg_match returns an int that says how many times the pattern matches. So, it doesn't determine if the URLs are the same. It just checks if they are of any URL. Link to comment https://forums.phpfreaks.com/topic/199500-regular-expression-matching-issue/#findComment-1047061 Share on other sites More sharing options...
tomtimms Posted April 23, 2010 Author Share Posted April 23, 2010 Anyone have any insight on how this could be accomplished? Link to comment https://forums.phpfreaks.com/topic/199500-regular-expression-matching-issue/#findComment-1047071 Share on other sites More sharing options...
Ken2k7 Posted April 23, 2010 Share Posted April 23, 2010 strcmp Link to comment https://forums.phpfreaks.com/topic/199500-regular-expression-matching-issue/#findComment-1047077 Share on other sites More sharing options...
dirkers Posted April 23, 2010 Share Posted April 23, 2010 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: http://somesite.com https://somesite.com http://www.somesite.com https://www.somesite.com -D Link to comment https://forums.phpfreaks.com/topic/199500-regular-expression-matching-issue/#findComment-1047243 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.