Reputation: 75
I'm trying to reach only the the site name and the domain, for example in case of images.google.com/thisIsJustAnExample it will return google.com. this is what I tried to do,
$url = 'https://images.google.com/thisIsJustAnExample';
$zobrazeni = array();
$zobrazeni = parse_url($url);
$zobrazeni["host"] = explode(".", $zobrazeni["host"]);
$zobrazeni["host"] = $zobrazeni["host"][count($zobrazeni["host"])-2] .".". $zobrazeni["host"][count($zobrazni["host"])-1];
And this is the Error I get:
Notice: Undefined offset: -1 in C:\xampp\htdocs\retezce.php on line 25
Notice: Undefined variable: zobrazni in C:\xampp\htdocs\retezce.php on line 25
Notice: Undefined offset: -1 in C:\xampp\htdocs\retezce.php on line 25
Upvotes: 0
Views: 73
Reputation: 6770
There are a few ways to do this but one would be:
$url = 'https://images.google.com/thisIsJustAnExample';
$zobrazeni = array();
$zobrazeni = parse_url($url);
$zobrazeni = explode(".", $zobrazeni["host"]);
$zobrazeni = array_reverse($zobrazeni);
$zobrazeni = $zobrazeni[1] . ' .' . $zobrazeni[0];
Or you could get the length of the array and use that instead of trying to subtract 2.
Upvotes: 0
Reputation: 871
just play with this code, it should print "google"."com" if thats what you meant.
just remember that count($verb)
(in case $verb
is array
) will return the number of the objects in the array.
$url = "http://stackoverflow.com/questions/29731201/getting-2-last-elements-of-a-url-address/29731258#29731258";
$verbs = explode('.', $url);
$domainName = explode('/', $verbs[count($verbs)-2]);
$domainExt = explode('/', $verbs[count($verbs)-1]);
echo $domainName[2] . "." . $domainExt[0];
Upvotes: 1