blacktiger786 Posted October 19, 2013 Share Posted October 19, 2013 hi friend i need you help i want create a form where when i add a domain its store to database but problem is i want only without http:// or www domains save in database other wise show error please enter domain without http:// or www please tell me what php code for this validation thanks in advanced Link to comment https://forums.phpfreaks.com/topic/283095-domain-url-validation-help/ Share on other sites More sharing options...
vinny42 Posted October 19, 2013 Share Posted October 19, 2013 The best ways is probably a regular expression. You can remove http:// using str_replace because that can only ever appear at the start of the URL, but the 'www' must only be removed if it appears at the start of the URL and ends with a dot. Otherwise you would break domainnames like 'thisismywwwdomain.com' something like: $strURl = preg_replace('~^www\.~','',$strURI); Link to comment https://forums.phpfreaks.com/topic/283095-domain-url-validation-help/#findComment-1454520 Share on other sites More sharing options...
Irate Posted October 19, 2013 Share Posted October 19, 2013 Try parse_url, too. You can just easily get the host name with that. Link to comment https://forums.phpfreaks.com/topic/283095-domain-url-validation-help/#findComment-1454522 Share on other sites More sharing options...
QuickOldCar Posted October 19, 2013 Share Posted October 19, 2013 A simple example and function. <?php function getparsedHost($new_parse_url) { if(!$new_parse_url){ return NULL; } if(!preg_match("~://~",$new_parse_url)){ $new_parse_url = "http://".$new_parse_url; } $parsedUrl = parse_url(trim(strtolower($new_parse_url))); $parsedUrl = str_replace("www.",'',$parsedUrl); return trim($parsedUrl['host'] ? $parsedUrl['host'] : array_shift(explode('/', $parsedUrl['path'], 2))); } //Parsing hosts from array in a loop example: $url_array = array("google.com","http://google.com","http://images.google.com","google.com/images","ftp://cdn.google.com","HTTP://WWW.GOOGLE.COM","","http://google.co.uk/images","blah.blah.blah.blah"); returns: google.comgoogle.comimages.google.comgoogle.comcdn.google.comgoogle.comgoogle.co.ukblah.blah.blah.blah As can see don't actually check if is a valid top or second level domain, would need to do checking after if needed that. Also have a function made to get main domains as well, is quite a chunk of code. Link to comment https://forums.phpfreaks.com/topic/283095-domain-url-validation-help/#findComment-1454545 Share on other sites More sharing options...
QuickOldCar Posted October 19, 2013 Share Posted October 19, 2013 No idea why the rest of code didn't post, i surely like the old way here on phpfreaks. <?php function getparsedHost($new_parse_url) { if(!$new_parse_url){ return NULL; } if(!preg_match("~://~",$new_parse_url)){ $new_parse_url = "http://".$new_parse_url; } $parsedUrl = parse_url(trim(strtolower($new_parse_url))); $parsedUrl = str_replace("www.",'',$parsedUrl); return trim($parsedUrl['host'] ? $parsedUrl['host'] : array_shift(explode('/', $parsedUrl['path'], 2))); } //Parsing hosts from array in a loop example: $url_array = array("google.com","http://google.com","http://images.google.com","google.com/images","ftp://cdn.google.com","HTTP://WWW.GOOGLE.COM","","http://google.co.uk/images","blah.blah.blah.blah"); Link to comment https://forums.phpfreaks.com/topic/283095-domain-url-validation-help/#findComment-1454547 Share on other sites More sharing options...
QuickOldCar Posted October 19, 2013 Share Posted October 19, 2013 Any reason why I can't post the entire code in here mods? <?phpfunction getparsedHost($new_parse_url) { if(!$new_parse_url){ return NULL; } if(!preg_match("~://~",$new_parse_url)){ $new_parse_url = "http://".$new_parse_url; } $parsedUrl = parse_url(trim(strtolower($new_parse_url))); $parsedUrl = str_replace("www.",'',$parsedUrl); return trim($parsedUrl['host'] ? $parsedUrl['host'] : array_shift(explode('/', $parsedUrl['path'], 2))); } //Parsing hosts from array in a loop example: $url_array = array("google.com","http://google.com","http://images.google.com","google.com/images","ftp://cdn.google.com","HTTP://WWW.GOOGLE.COM","","http://google.co.uk/images","blah.blah.blah.blah");foreach($url_array as $urls){echo getparsedHost($urls)."<br />";}?> Link to comment https://forums.phpfreaks.com/topic/283095-domain-url-validation-help/#findComment-1454548 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.