How To Convert URL To Array In PHP Parse_url Function Example

PHP 8 , PHP

Nicesnippets

358

02-09-2021


How To Convert URL To Array In PHP Parse_url Function Example

Hi guys,

Today i will explained How To Convert URL To Array In PHP Parse_url Function. This example is so easy to use in php.

An URL is the string of many data. URL contains domain, host, parameters scheme, path etc. When you are handling external source URL, you may want to validate domain and schemen or get data from query string.

PHP has in-built parse_url function which returns associative array of url components from the url. Invalid URL returns as false. So let's start to the example.

Syntax :


parse_url($url, $component);

Parameters :

$url URL which you want to be parsed

$component optional specific component name to be return instead of all components(PHP_URL_SCHEME, PHP_URL_HOST, PHP_URL_PORT, PHP_URL_USER, PHP_URL_PASS, PHP_URL_PATH, PHP_URL_QUERY and PHP_URL_FRAGMENT)

Example :

The below example returns the following url components from the url.

<?php

$url = 'https://username:[email protected]:8800/user/data?limit=5&skip=100#stop_end';

print_r(parse_url($url));

?>

Output

Array (

[scheme] => https

[host] => api.nicesnippets.com

[port] => 8800

[user] => username

[pass] => 123456

[path] => /user/data

[query] => limit=5&skip=100

[fragment] => stop_end

)

With this response you can validate whether the domain is SSL certified or not. You can also get get parameters from the URL using this method.

Now you can check your own.

I hope it can help you...


Recommended Posts