1

Possible Duplicate:
explode without variables

$domainArray = explode('.', $_SERVER['HTTP_REFERER']);
$domainname = $domainArray[1];

I have to run these 2 lines of code on many pages of a site - above code works fine.

I want to change it into one line and have tried many times unsuccessfully.

Any ideas?

Community
  • 1
  • 1
Adam
  • 19,932
  • 36
  • 124
  • 207
  • 1
    What version of PHP are you running? – afuzzyllama Dec 25 '11 at 05:43
  • 3
    eh why exactly does it need to be one line? – Breton Dec 25 '11 at 05:48
  • 1
    If you have this on many pages then you'd better write a function for it that has a name that reflects the meaning of the code, e.g. getDomainName(). Then it does not really matter any more how many lines it will take, and you can write a nice PHPDoc description for it. – The Nail Dec 25 '11 at 10:30

5 Answers5

3

with PHP 5.4 you can write just: $domainArray = explode('.', $_SERVER['HTTP_REFERER'])[1]

also you can write function like this

function getByIndex($array, $index) {
    return $array[$index];
}

$domainname = getByIndex(explode('.', $_SERVER['HTTP_REFERER']), 1);
knkill_prfl
  • 219
  • 1
  • 4
  • 9
3

This should work with PHP < 5.4:

list(,$domainname) = explode('.', $_SERVER['HTTP_REFERER']);
vstm
  • 12,407
  • 1
  • 51
  • 47
1

I'd just make it a function and have it return the Domain Name...

ReadWriteCode
  • 664
  • 4
  • 7
1

PHP < 5.4

// 2 is the index of the element you want to access
current(array_slice(explode(",", $input), 2, 1)));
afuzzyllama
  • 6,538
  • 5
  • 47
  • 64
1

This isn't exactly the same because: -yours strips the leftmost subdomain -returns the tld in the event of no subdomain

anyway

echo $host = parse_url($_SERVER['HTTP_REFERRER'], PHP_URL_HOST);
goat
  • 31,486
  • 7
  • 73
  • 96