简体   繁体   中英

clean the url in php

I am trying to make a user submit link box. I've been trying all day and can't seem to get it working.

The goal is to make all of these into example.com... (ie. remove all stuff before the top level domain)

Input is $url =

Their are 4 types of url:

www.example.com...
example.com...
http://www.example.com...
http://example.com...

Everything I make works on 1 or 2 types, but not all 4.

How one can do this?

You can use parse_url for that. For example:

function parse($url) {
    $parts = parse_url($url);
    if ($parts === false) {
        return false;
    }
    return isset($parts['scheme']) 
            ? $parts['host'] 
            : substr($parts['path'], 0, strcspn($parts['path'], '/'));
}

This will leave the "www." part if it already exists, but it's trivial to cut that out with eg str_replace . If the url you give it is seriously malformed, it will return false .

Update (an improved solution):

I realized that the above would not work correctly if you try to trick it hard enough. So instead of whipping myself trying to compensate if it does not have a scheme, I realized that this would be better:

function parse($url) {
    $parts = parse_url($url);
    if ($parts === false) {
        return false;
    }
    if (!isset($parts['scheme'])) {
        $parts = parse_url('http://'.$url);
    }
    if ($parts === false) {
        return false;
    }

    return $parts['host'];
}

Your input can be

output is example.com

there's a few steps you can take to get a clean url.

Firstly you need to make sure there is a protocol to make parse_url work correctly so you can do:

//Make sure it has a protocol
if(substr($url,0,7) != 'http://' || substr($url,0,8) != 'https://')
{
    $url = 'http://' . $url;
}

Now we run it through parse_url()

$segments = parse_url($url);

But this is where it get's complicated because the way domain names are constructed is that you can have 1,2,3,4,5,6 .. .domain levels, meaning that you cannot detect the domain name from all urls, you have to have a pre compiled list of tld's to check the last portion of the domain, so you then can extract that leaving the website's domain.

There is a list available here : http://mxr.mozilla.org/mozilla-central/source/netwerk/dns/effective_tld_names.dat?raw=1

But you would be better of parsing this list into mysql and then select the row where the tld matches the left side of the domain string.

Then you order by length and limit to 1, if this is found then you can do something like:

$db_found_tld = 'co.uk';
$domain = 'a.b.c.domain.co.uk';
$domain_name = substr($domain,0 - strlen($db_found_tld));

This would leave abcdomain , so you have removed the tld, now the domain name would be extracted like so:

$parts = explode($domain_name);
$base_domain = $parts[count($parts) - 1];

now you have domain .

this seems very lengthy but I hope now you know that its not easy to get just the domain name without tld or sub domains.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM