Regex To Grab Domain From Url

Fellow Php Programmers,

I found this regex to grab domain name of urls:

/^(?:http(?:s)?://)?(?:[^.]+.)?example.com$/

Found it here:

It will grab domain name from the following type of urls:

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

I also found the following code but it is not working:


<?php

$domain = parse_url($url, PHP_URL_HOST);

echo $domain;

?>

Found it here:

And now found this:

$domain = parse_url(‘http://’ . str_replace(array(‘https://’, ‘http://’), ‘’, $url), PHP_URL_HOST);

Do you have any better suggestion that will not go a miss on any form of url so the domain grabbing would be a 100% guarantee with your suggested code ?

In what way is it not working?

1 Like

To expand on @Gandalf’s question, what is the value of $url that you are passing into parse_url?

2 Likes

I see a complete blank page.
I see SpaceShipTrooper LIKED your question. I am glad my old friend is back!
Missed you old buddy!

Ha! Ha! I see SpaceShipTrooper LIKING this post too. Good. He is getting active in my threads, again. :wink:

Actually, I was about to go out and found this link:

I then just created a webpage on my website. Eg:

http://example.com/grab_domain_name.php

And pasted this code, which I grabbed from that link:

$domain = parse_url($url, PHP_URL_HOST);

And, I saw a blank page. Couldn’t experiment with the code any more due to short time.
Anyway, I now tried these 2 codes which I made-up myself, figuring those is how they should have been:


<?php

$url = http://google.com;
$domain = parse_url($url, PHP_URL_HOST);
echo $domain;

?>

<?php

$domain = parse_url(http://google.com, PHP_URL_HOST);
echo $domain;

?>

Again, blank page.
I know I am doing something wrong and so go easy on the crticisms! :wink:
Let me see how you guys would code it! :wink:

You are missing quotes.
$url = "http://google.com";
And also turn on error reporting.

2 Likes

I tried the following approx. 5 mins before you suggested the quotes but no luck:


<?php

$domain = parse_url("http://google.com", PHP_URL_HOST);
echo $domain;

?>

<?php

$domain = parse_url('http://google.com', PHP_URL_HOST);
echo $domain;

?>

Did you enable error reporting?

I just tested with all this now and page still complete a white blank (my website):


<?php

/*
ERROR HANDLING
*/
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);

//For All Error, Warning and Notice
error_reporting(E_ALL) OR error_reporting(-1);
//For All Errors
error_reporting(E_ERROR);
//For All Warnings
error_reporting(E_WARNING);
//For All Notice
error_reporting(E_NOTICE);

error_reporting(E_ALL);



$url = "http://google.com";
$domain = parse_url($url, PHP_URL_HOST);
echo $domain;


$domain = parse_url(http://google.com, PHP_URL_HOST);
echo $domain;


$domain = parse_url("http://google.com", PHP_URL_HOST);
echo $domain;


$domain = parse_url('http://google.com', PHP_URL_HOST);
echo $domain;

?>

However, my xampp shows this error:

Parse error: syntax error, unexpected ‘:’, expecting ‘,’ or ‘)’ in C:\xampp\htdocs\id\grab_domain.php on line 27

$domain = parse_url('http://google.com', PHP_URL_HOST);
echo $domain;

works fine. you are doing something wrong. I just put that in a php file, and it ran just fine.

1 Like

Yeah. It is working for me now too!
Look at my previous post. Can you see, the last few lines look like this:


$domain = parse_url(http://google.com, PHP_URL_HOST);
echo $domain;
$domain = parse_url("http://google.com", PHP_URL_HOST);
echo $domain;
$domain = parse_url('http://google.com', PHP_URL_HOST);
echo $domain;

Can you spot what I am doing wrong here ?
I missed the quotes on the first mention of google.com. Fixed that and it is working.
I get my xampp page showing this now …

google.comgoogle.comyahoo.comebay.com

The code was this:


<?php

/*
ERROR HANDLING
*/
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);

//For All Error, Warning and Notice
error_reporting(E_ALL) OR error_reporting(-1);
//For All Errors
error_reporting(E_ERROR);
//For All Warnings
error_reporting(E_WARNING);
//For All Notice
error_reporting(E_NOTICE);

error_reporting(E_ALL);



$url = "http://google.com";
$domain = parse_url($url, PHP_URL_HOST);
echo $domain;


$domain = parse_url("http://google.com/1", PHP_URL_HOST);
echo $domain;


$domain = parse_url("http://yahoo.com/2", PHP_URL_HOST);
echo $domain;


$domain = parse_url('http://ebay.com/3', PHP_URL_HOST);
echo $domain;

?>

Note the last one has single quote while the other 2 double.

But, all this is related to “parse_url” built-in function. And that could fail on some urls. Therefore, still on the hunt for the perfect regex.

Its outputing everything on same line, add <br> tag for each echo.

1 Like

Yes, but as with all of your topics you change directions every 5 minutes. Your code started with $url instead of a hard-coded value. Provided $url was actually being set and sent to the function, quotes were not required, as it was a variable and would be interpretted as a string already.

Now that you have hard-coded it, yes, quotes are absolutely necessary, no it doesn’t matter if it is single or double quotes.

If you wish to know why, or how single or double quotes behave differently in PHP, I strongly recommend you borrow or buy a book and read it. You are not going to go very far if you keep just copying and pasting random code you find on the internet into your project.

2 Likes

Not bothered adding br tags because that was just a rough test. In my actual webpage, ofcourse br tags would be added.
Thanks for the suggestion, though.

Don’t worry, I know about single and double quotes.
Look this doesn’t work:

$url = http://one.com/1.php;

I get error:
Parse error: syntax error, unexpected ‘:’ in C:\xampp\htdocs\id\grab_domain.php on line 22

Therefore, had to do it like this:

$url = “http://one.com/1.php”;

And, even this works:

$url = ‘http://one.com/1.php’;

But for some reason, I still get blank page on my website.
It is only working on xampp.
Here’s the updated code that is working …


<?php

/*
ERROR HANDLING
*/
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);

//For All Error, Warning and Notice
error_reporting(E_ALL) OR error_reporting(-1);
//For All Errors
error_reporting(E_ERROR);
//For All Warnings
error_reporting(E_WARNING);
//For All Notice
error_reporting(E_NOTICE);

error_reporting(E_ALL);



$url = "http://one.com/1.php";
$domain = parse_url($url, PHP_URL_HOST);
echo $domain;

$url = "http://two.com/2.htm";
$domain = parse_url($url, PHP_URL_HOST);
echo $domain;


$domain = parse_url("http://three.com/3.html", PHP_URL_HOST);
echo $domain;


$domain = parse_url("http://four.com/4.shtm", PHP_URL_HOST);
echo $domain;


$domain = parse_url('http://five.com/5.shtml', PHP_URL_HOST);
echo $domain;

?>

Note, the page extensions I experimented with.
Yes, when my code doesn’t work, I start experimenting. Hence, you see a sudden shift in direction. And, a GREAT shift!
I see this result on xampp (Don’t advise about the br tags. Remember, this is just rough experimenting).

one.comtwo.comthree.comfour.comfive.com

Yes, because you are hard-coding a value into the variable $url.

No, that is definitely not it :wink:
What you are doing isn’t experimenting, it is trying to learn through brute force, which is a horrible way to learn, but “to each their own.”

If you had read the manual over parse_url, you would have already known the extension doesn’t matter, especially if you are asking for the PHP_URL_HOST specifically. Not to mention parse_url doesn’t pull out the extension, it pulls out the path, which would be the filename in your examples above.

But if you need a clear example of the constant direction changing and/or the ambiguity of your postings, look no further than this:

What am I supposed to take away from that? It doesn’t work on my website, it works on xampp, here is the updated code that is working… working where? xampp or on your website or both? You have three sentences that want to be related to each other, but aren’t in a meaningful way.

If it isn’t working on your website, you need to figure out how to get verbose error logging to work on your website so you can start to figure things out.

And if I had to take a wild guess, I’d bet that the code you put on your website is more than

<?php
$domain = parse_url('http://google.com', PHP_URL_HOST);
echo $domain;

Which means you likely compounded your other problems into this one. But what happens if you only upload the above in a script to your website?

1 Like

I already glanced over this quickly about an hr ago:

http://php.net/manual/en/function.parse-url.php

But I don’t give much time to the manual because the complicated codes put me off from php. Hence, I exit as quick as I entered the site.
Yeah, I knew the extensions don’t matter but I still wanted to experiment and see for myself.

Come-on, don’t tell me you did not understand this:

But for some reason, I still get blank page on my website.
It is only working on xampp.
Here’s the updated code that is working …

It means, the updated code is only working on xampp. Not on the website. Have to open a ticket with the webhost about this.

And this, only works in xampp too:


<?php
$domain = parse_url('http://google.com', PHP_URL_HOST);
echo $domain;

In short, whenever I tested a snippet of code or my updated codes, I tested on both offline and online servers.

What do you mean by hard coding ? You mean turning the value fixed, like a constant or string, by adding quotes ?

Off-topic: What is cpradio ? Sounds cpa radio. See if cparadio.com or cpa-radio.com is available or not. Imagine a website where people listen to radio and the sound publishers earn money. Cpa income. Oh forget it. I’m already swimming in too many ideas which I want to bring to life with php. Best not to “shift direction” towards “radio” now. But again, incase you are wondering …
Imagine a site like youtube where people don’t upload videos but audios (podcasts, etc.) and publishers earn cpa income from the visits. Or, imagine you opened a cpa ad publishing company that provides publishers with advertiser podcasts and you called your company cparadio.
Don’t bother replying to this. Just mentioned it, incase anyone wants to develope the idea.

It is hard to take your problems serious when there are responses like this. If you do not wish to put in the time to learn, there isn’t much point in helping you.

What version of PHP is your host utilizing? Who is your host? Have you checked their control panel for where their Access/Error logs may be stored?

4 Likes

In other words, it is a constant sort of thingy …

Sure we can go with that summary, if not taking that literally to reflect as an actual constant variable that most languages support (don’t confuse the two, even though they are similar).

1 Like