Remove everything after the domain

#1

I’m trying to echo out a domain from the database, well could be hundreds of them, so am looking at parse_url but I’m not sure how best to use it.

So have made a start below which isnt right, but how do I use the array it creates to echo out the domain as I need it.

This is how it currently is:

<a href="http://<?php echo $row['Web_Hot']?>">http://<?php echo $row['Web_Hot']?></a>

But some of the url’s are huge and really dont need anything after the .com or whatever, so started with this.

<a href="http://<?php echo $row['Web_Hot']?>">http://<?php echo parse_url($row['Web_Hot'])?></a>

What more do i need to do to get it to work with parse_url

#2

Well, you’ve already given a function by name, so why not look at the manual for that function? (Specifically, the return values of the function may be of particular interest to you. Does it ALWAYS return an array?)

#3

mmm, I cant get it, so I tried this below as there not http:// at the start, so thought this might work, but its says I’m using invalid arguments passed in on implode().

$url = $row['Web_Hot'];
echo $url;
$parts = explode('/', $url);
$output = implode('/', array_slice($url, 0, 1));

echo $output;

This is what echo $url echo’s out

www.thisisright.com/hotel/EN/ficha_hotel/aqua_fun_marrakech/descripcion.jsp

Silly, I needed to put $parts in implode and not $url, will check it again now

#4

Here we go, this works.

$url = $row['Web_Hot'];
$parts = explode('/', $url);
$output = implode('/', array_slice($parts, 0, 1));
#5

From the manual:

Return Values

On seriously malformed URLs, **parse_url()** may return **`FALSE`** . ....etc etc etc

If the component parameter is specified, parse_url() returns a string (or an integer, in the case of PHP_URL_PORT ) instead of an array. If the requested component doesn’t exist within the given URL, NULL will be returned.

#6

Be careful. This depends on requirements. For example what about www.amazon.co.uk? There are other TLDs that, as I recall, are even more unusual. So depending on requirements you might need to use something that is able to parse domain names properly.