w3resource

PHP Exercises : Return some components of an url

PHP : Exercise-8 with Solution

Write a PHP script, which will return the following components of the url 'https://www.w3resource.com/php-exercises/php-basic-exercises.php'.

List of components : Scheme, Host, Path

What is a URL?

URL stands for Uniform Resource Locator. It is used to specify its location on a computer network and a mechanism for retrieving it. A URL is the fundamental network identification for any resource connected to the web (e.g., hypertext pages, images, and sound files).

URLs have the following format:

protocol://hostname/other_information

For example, the URL for w3resource's PHP basic exercises page is:

'https://www.w3resource.com/php-exercises/php-basic-exercises.php';

Sample Solution: -

PHP Code:

<?php
$url = 'https://www.w3resource.com/php-exercises/php-basic-exercises.php';
$url=parse_url($url);
echo 'Scheme : '.$url['scheme']."\n";
echo 'Host : '.$url['host']."\n";
echo 'Path : '.$url['path']."\n";
?>

Sample Output:

Scheme : http                                               
Host : www.w3resource.com                                   
Path : /php-exercises/php-basic-exercises.php

Flowchart:

Flowchart: Return some components of an url

PHP Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a PHP script to get the current file name.
Next: Write a PHP script, which changes the color of the first character of a word.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.

PHP: Tips of the Day

Returns all elements in an array except for the first one

Example:

<?php
function tips_tail($items)
{
  return count($items) > 1 ? array_slice($items, 1) : $items;
}

print_r(tips_tail([1, 5, 7]));
?> 

Output:

Array
(
    [0] => 5
    [1] => 7
)