• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / PHP / How to Parse URLs with php parse_url() Function

How to Parse URLs with php parse_url() Function

April 4, 2022 Leave a Comment

To parse a URL in php, you can use the php parse_url() function and get an array with the URL scheme, host, path, query string, and other information.

$url = "http://theprogrammingexpert.com/php-parse-url?user=friend#hello";

print_r(parse_url($url));

//Output:
Array
(
    [scheme] => https
    [host] => theprogrammingexpert.com
    [path] => /php-parse-url
    [query] => user=friend
    [fragment] => hello
)

If you want to only get a specific piece of information about a URL, you can pass a value to the optional ‘component’ parameter.

$url = "http://theprogrammingexpert.com/php-parse-url?user=friend#hello";

echo parse_url($url, PHP_URL_SCHEME) . "\n";
echo parse_url($url, PHP_URL_HOST) . "\n";
echo parse_url($url, PHP_URL_PATH) . "\n";
echo parse_url($url, PHP_URL_QUERY) . "\n";
echo parse_url($url, PHP_URL_FRAGMENT) . "\n";

//Output:
https
theprogrammingexpert.com
/php-parse-url
user=friend
hello

When working with URLs in our programs, the ability to parse a URL and extract information about the URL is valuable.

In php, the parse_url() function allows us to easily extract information about a given URL.

We can use parse_url() to parse URLs and get information about the URL scheme, host, port, path, query and fragment.

Below is a simple example of how to use parse_url() to parse a URL in php.

$url = "http://theprogrammingexpert.com/php-parse-url?user=friend#hello";

print_r(parse_url($url));

//Output:
Array
(
    [scheme] => https
    [host] => theprogrammingexpert.com
    [path] => /php-parse-url
    [query] => user=friend
    [fragment] => hello
)

Getting a Specific URL Component with parse_url() in php

With parse_url(), you can get a specific component of a URL by passing a value to the optional ‘component’ parameter of parse_url().

If the component you are looking for exists, parse_url() will return a string. If the component doesn’t exist, parse_url() will return NULL.

Below are a few examples of how to get specific components from a URL in php.

$url = "http://theprogrammingexpert.com/php-parse-url?user=friend#hello";

echo parse_url($url, PHP_URL_SCHEME) . "\n";
echo parse_url($url, PHP_URL_HOST) . "\n";
echo parse_url($url, PHP_URL_PATH) . "\n";
echo parse_url($url, PHP_URL_QUERY) . "\n";
echo parse_url($url, PHP_URL_FRAGMENT) . "\n";

//Output:
https
theprogrammingexpert.com
/php-parse-url
user=friend
hello

Hopefully this article has been useful for you to learn how to parse a URL with the php parse_url() function.

Other Articles You'll Also Like:

  • 1.  Replace Underscore with Space in php String
  • 2.  php preg_match – Find Pattern in String Using Regex (Regular Expression)
  • 3.  Creating Custom category.php With Pagination
  • 4.  php e – Using M_E and exp() Function to Get Euler’s Constant e
  • 5.  Remove Duplicates from Array in php with array_unique()
  • 6.  How to Get Current System IP Address in php
  • 7.  php atan2 – Find Arctangent of the Quotient of Two Numbers
  • 8.  php sleep() Function – Pause Execution of Code in php
  • 9.  php str_split() – Convert String into Array of Equal Length Substrings
  • 10.  Get Current Location Latitude Longitude in php

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy