xRobot
xRobot

Reputation: 26567

How to extract the website's URL from a page's URL?

I have to extract the website's URL from a page's URL. For example, this is my php code:

<?php

$pageA = "http://stackoverflow.com/questions/tagged/php";
$pageB = "https://www.google.it/search?channel=cs&ie=UTF-8&q=php+download";
$pageC = "http://www.facebook.com/ladygaga";
$pageD = "www.youtube.com";
$pageE = "yahoo.com";

?>

and I have to extract

stackoverflow.com
www.google.it
www.facebook.com
www.youtube.com
yahoo.com

from these page's URL.

Upvotes: 0

Views: 241

Answers (1)

Vikas
Vikas

Reputation: 8948

parse_url is exactly for this. From the linked manual:

This function parses a URL and returns an associative array containing any of the various components of the URL that are present.

Example:

<?php
$url = 'http://username:password@hostname/path?arg=value#anchor';

print_r(parse_url($url));
?>

Will output:

Array
(
  [scheme] => http
  [host] => hostname
  [user] => username
  [pass] => password
  [path] => /path
  [query] => arg=value
  [fragment] => anchor
)

Update

Cases where there are no scheme parse_url fails to recognize other fields. A workaround to this problem is to catch this case, append a default scheme and reparse. Code would look something like:

<?php
$url = 'yahoo.com/help';
$fields = parse_url($url);

if(empty($fields['scheme'])) {
  $fields = parse_url('http://' . $url);
}

print_r($fields);
?>

Upvotes: 1

Related Questions