-3

This code works:

    $host = parse_url('http://www.cc.joomla.mywebsite.net/paypal.com/myspace.com/login.php', PHP_URL_HOST);

    $host_names = explode(".", $host);
    print_r($host_names);
    echo "<br>";
    $l = array_slice($host_names, -3);
    print_r($l);
    echo "<br>";

    $subdomain = implode(".", $l);
    echo $subdomain;    //final result


Is it possible to combine function in one line for example like this:

$subdomain = implode(array_slice (explode(".", $host)($host_names, -3)(".", $l);

The example above does not worked. I think I have seen it before where you can combine function.

Thanks in advance for any help.

5
  • 2
    You either have a hard time to formulate the expression syntactically correct or you just want to learn about writing your own php.net/language.functions <- I suggest you the latter. Because the first one is self-mockery and so you should do it your own to make you really proud. Having others write you one-liners is considered cheating in this browser game here. :D Commented Aug 21, 2013 at 10:25
  • Also, what is the goal of your function? Just to get the subdomain? Commented Aug 21, 2013 at 10:27
  • 1
    you might want to take a look at this stackoverflow.com/questions/535336/…, anyway :) Commented Aug 21, 2013 at 10:27
  • @hakre -I don't know how to combine function. deceze below explains nicely what I need. Commented Aug 21, 2013 at 12:43
  • @Fabien - yes to get the subdomain. Commented Aug 21, 2013 at 12:44

4 Answers 4

2

Just substitute each variable with the expression that you assign to it:

$host_names = explode(".", $host);
$l = array_slice($host_names, -3);

becomes

$l = array_slice(explode(".", $host), -3);

Then

$l = array_slice(explode(".", $host), -3);
$subdomain = implode(".", $l);

becomes

$subdomain = implode(".", array_slice(explode(".", $host), -3));
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for the detail explanation. exactly the information I need.
hmm, this does not do the print_r's in question and does not echo the br's. I wonder how it matches the requirement in the question ... . ;)
the print_r and <br> is just for example. I just need to know how to combine multiple function in single line.
1

You are mixing all your 3 lines into one, but it should be in this format to combine output of 1 function into another.

$subdomain = implode(".",array_slice(explode(".", $host), -3));

Comments

1

You can chain funktions "like" that. But if you don't take care they care getting unreadably. Like yours and then errors are difficult to be found:

implode(array_slice (explode(".", $host)/* this is not a valid function call */($host_names, -3) /* same here*/ (".", $l);

There are so many function calls that you really don't want them all into one line. Its unreadable.

You probably want to write your own function.: http://php.net/manual/en/functions.user-defined.php

Comments

1

please try this:

$host_names = implode(".", array_slice(explode(".", $host), -3));

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.