PHP | parse_url() Function Example

03-Apr-2023

.

Admin

PHP | parse_url() Function Example

Hi Friends,

This example is PHP | parse_url() Function Example.

The parse_url() function is an inbuilt functionin PHP which is used to return the components of a URL by parsing it.

It parse an URL and return an associative array which contains its various components.

Let's start following example.

Syntax:


parse_url( $url, $component = -1 )

Example 1:

<?php

// Declare a variable and initialize it with URL

$url = 'https://nicesnippets.com/php/#basics';

// Use parse_url() function to parse the URL

var_dump(parse_url($url));

var_dump(parse_url($url, PHP_URL_SCHEME));

?>

Output:

array(4) {

["scheme"]=>

string(5) "https"

["host"]=>

string(16) "nicesnippets.com"

["path"]=>

string(5) "/php/"

["fragment"]=>

string(6) "basics"

}

string(5) "https"

Example 2:

<?php

// Declare a variable and initialize it with URL

$url = '//www.nicesnippets.com/path?php=PHP';

// Use parse_url() function to

// parse the URL

var_dump(parse_url($url));

?>

Output:

array(3) {

["host"]=>

string(20) "www.nicesnippets.com"

["path"]=>

string(5) "/path"

["query"]=>

string(7) "php=PHP"

}

I hope it can help you....

#PHP 8

#PHP