get domain name from URL in php

Posted on 2011-04-29
Last Modified: 2012-05-11
I'm trying to write a code that extracts the domain name from a list of URL's. URL's are written in a txt file.  following code works for most of the URL's but not with etc

	$lines = file($_FILES['domainUploadFile']['tmp_name']);
	foreach ($lines as $line_num => $url) {
		$url, $matches);
		$host = $matches[1];
		// get last two segments of host name
		preg_match('/[^.]+\.[^.]+$/', $host, $matches);
		echo "domain name is: {$matches[0]}\n"."<br />\n";
	   //echo "Line #<b>{$line_num}</b> : " . htmlspecialchars($line) . "<br />\n";

Open in new window

this is the input

see the output

domain name is:
domain name is:
domain name is:
domain name is:
domain name is:
domain name is:
Question by:hiddenpearls
    LVL 48

    Expert Comment

    Or simply use in your loop:
    echo str_replace('www.', '', parse_ulr($url, PHP_URL_HOST));

    Author Comment

    PHP_URL_HOST doesn't work when url is  means without http://
    LVL 74

    Expert Comment

    by:käµfm³d 👽
    This is an extremely difficult thing to do with regex, as you have already witnessed. URLs can have multiple parts, and TLDs can be anywhere from 2 to 6 (maybe more) characters long and themselves consist of multiple parts.

    Is there any way to categorize the URLs into general categories of how the URLs are constructred? For example, given your above example, we could say you have 2 categories:

    LVL 30

    Assisted Solution

    by:Marco Gasi
    Try with regex:

    $subject = "";
    preg_match('/\b([a-z0-9]+(-[a-z0-9]+)*\.)+[a-z]{2,}\b/ix', $subject, $matches);

    echo "<pre>";

    To get individual items do

    foreach ($matches[0] as $domain){
      echo $domain . "<br />";

    LVL 14

    Accepted Solution

    this is ugly code but can be cleaned up to do what you want it to do, I think.  It works with your data set.
    $a[]= '';
    $a[]= '';
    $a[]= '';
    $a[]= '';
    $a[]= '';
    $a[]= '';
    foreach ($a as $url){
    	$x = parse_url($url);
    	// print_r($x);
    	if (array_key_exists('host', $x)) {
    		$parts = explode('.', $x['host']);		
    		$new_url1 = array_pop($parts);
    		$new_url = array_pop($parts).'.'.$new_url1;
    	} else if (array_key_exists('path',$x)){ 
    		$parts = explode('.', $x['path']);
                    // get last two parts of the domain name	
    		$part1 = array_pop($parts);
    		$part1 = array_pop($parts).'.'.$part1;
                    // account for potentail of trailing slash
    		$part2 = explode('/',$part1);
    		$new_url = $part2[0];
    	} else {
    		echo 'invalid';
    	echo PHP_EOL;	

    Open in new window

    LVL 30

    Expert Comment

    by:Marco Gasi
    Can you give some feedback, please?
    LVL 107

    Expert Comment

    by:Ray Paseur
    Looking at this that you want to turn into this makes me wonder why you want to discard the "en" part of the name?  The subdomain is fairly important, as is banksy in  

    What is the desired output from the examples?

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    These days socially coordinated efforts have turned into a critical requirement for enterprises.
    The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    10 Experts available now in Live!

    Get 1:1 Help Now