Output is correct. when input is http://test.google.co.uk
value of parse_url('http://test.google.co.uk')['host']
is http://test.google.co.uk
. When you will exploce this string on dot first element of array will be test
and its length is 4.
To get google
instead of test
you need to replace subdomain with nothing as you did in your first example or take the second element in exploded string. E.g:
Example:
HTML
<?php
$url = 'http://test.google.co.uk';
$info = parse_url($url);
$pieces = explode(".", $info['host']);
$len = strlen($pieces[1]); // returns character length of google = 6
echo $len;
?>
Use parse_url()
. Something like:
Example:
HTML
<?php
$host = parse_url('http://www.google.co.uk/test.html');
preg_match('/(.*?)((\.co)?.[a-z]{2,4})$/i', $host['host'], $m);
$ext = isset($m[2]) ? $m[2]: '';
echo $ext; ?>
To retrieve all request headers using the getallheaders() function in PHP, you can follow these steps:
Example:
PHP
<?php
$all_headers = getallheaders();
foreach ($all_headers as $name => $value) {
echo "$name: $value";
}
?>