w3resource
php Programming Exercies

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?



New Content: Composer: Dependency manager for PHP, R Programming