I have the following code which is to extract the last to segments from a domain name:
example: xkcd.com
should be extracted from http://www.xckd.com
<?php
if( isset($_SERVER['HTTP_ORIGIN'])) {
$from = "host origin";
$origin_in = $_SERVER['HTTP_ORIGIN'];
preg_match('/[^.]+\.[^.]+$/', $origin_in, $matches);
$origin_out = $matches[0];
print_r($matches);
} else if($_SERVER['SERVER_NAME']) {
$from = "server name";
$origin_in = $_SERVER['SERVER_NAME'];
preg_match('/[^.]+\.[^.]+$/', $origin_in, $matches);
$origin_out = $matches[0];
}
header("Access-Control-Allow-Origin: *" );
echo "origin input: ". $origin_in. "
";
echo "origin output: ". $origin_out. "
";
echo "from: ". $from;
exit();
I have posted my code verbatim to be the most accurate representation of what is being processed as i am uncertain of where things are going wrong.
The output I get is
Array ( [0] => http://xxxx.com ) origin input: http://xxxx.com origin output: http://xxxx.com from: host origin
Why am I not getting xxxx.com
for the origin output?
My code can be contrasted against this snippet which works as expected: http://3v4l.org/nNKZn
Thanks for your help.