Jump to content

Recommended Posts

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

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);

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.com
google.com
images.google.com
google.com
cdn.google.com
google.com

google.co.uk
blah.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.

Edited by QuickOldCar

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");

Any reason why I can't post the entire code in here mods?

 

<?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");

foreach($url_array as $urls){
echo getparsedHost($urls)."<br />";
}
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.