-1

I have the following PHP code

$businessWebsite = 'http://www.times.hello.com.uk/articles/view/20.141013/local/police-killer.gets-10-more-months-over-drugs.539550';
$host = parse_url($businessWebsite, PHP_URL_HOST );
$parts = explode( '.', $host);
//$parts = array_reverse( $parts );

$domain = $parts[1].'.'.$parts[2].'.'.$parts[3].'.'.$parts[4];

print_r($parts);

echo $domain;

This echo's times.hello.com.uk this is made up of four parts

Array
(
    [0] => www
    [1] => times
    [2] => hello
    [3] => com
    [4] => uk
)

Let us say my domain is $businessWebsite = 'http://www.times.com.uk/articles/view/20.141013/local/police-killer.gets-10-more-months-over-drugs.539550';

It would echo times.hello.com.. I would end up with two dots at the end.

If the domain is $businessWebsite = 'http://www.times.com/articles/view/20.141013/local/police-killer.gets-10-more-months-over-drugs.539550';

It would echo times.com... I would end up with three dots at the end.

How do I go solving the problem?

I want to remove the double and triple dots at the end.

user2333968
  • 135
  • 1
  • 3
  • 13
  • What's your problem ? – Kevin Labécot Oct 13 '14 at 12:51
  • I want to remove the double and triple dots at the end. – user2333968 Oct 13 '14 at 12:52
  • 1
    Stop assuming that $parts[3] and $parts[4] actually exist, and actually check if they exist.... if they don't, don't echo them or the '.'.... why not use array_slice() to extract the parts of the array that you want to display (`$parts = str_slice(explode( '.', $host), 1);`), then implode with a '.' to create a single string that you can then echo – Mark Baker Oct 13 '14 at 12:52
  • if you need get domain from any URL, you can use REGEX: http://stackoverflow.com/questions/3442333/php-regex-get-domain-from-url – Thiago256 Oct 13 '14 at 12:56
  • @Thiago256 The accepted answer on that question suggests using `parse_url`. – Jim Oct 13 '14 at 13:16

3 Answers3

2

Use PHP's trim and implode.

As you said you want to remove double and triple dots at the end of the string:

$domain = implode('.',$parts);

$domain = trim($domain, '.');

print $domain;
1

Take a look at phps implode() function.

This allows to do something like this:

echo implode('.', $parts);
arkascha
  • 41,620
  • 7
  • 58
  • 90
1

PHP has an implode method:

$domain = implode('.',$parts);

You can remove the first item of parts prior to imploding:

array_shift($parts);
Jim
  • 22,354
  • 6
  • 52
  • 80