-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.

5
  • What's your problem ? Commented Oct 13, 2014 at 12:51
  • I want to remove the double and triple dots at the end. Commented Oct 13, 2014 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 Commented Oct 13, 2014 at 12:52
  • if you need get domain from any URL, you can use REGEX: stackoverflow.com/questions/3442333/… Commented Oct 13, 2014 at 12:56
  • @Thiago256 The accepted answer on that question suggests using parse_url. Commented Oct 13, 2014 at 13:16

3 Answers 3

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;
Sign up to request clarification or add additional context in comments.

3 Comments

For that: if($parts[0] == "www") { unset($parts[0]); }
After that you can implode it and trim it. Can you give it a try?
@user2333968 Thanks. Please tick mark the answer if its correct.
1

Take a look at phps implode() function.

This allows to do something like this:

echo implode('.', $parts);

Comments

1

PHP has an implode method:

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

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

array_shift($parts);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.