• 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
  • Ruby
  • 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.  php Convert Radians to Degrees with php rad2deg() Function
  • 2.  php Random Number Generator with rand Function
  • 3.  php Trig – Using Trigonometric Functions in php for Trigonometry
  • 4.  php preg_match_all – Get All Matches of Pattern in String
  • 5.  Using function_exists() to Check if Function Exists in php
  • 6.  php array_map() – Create New Array from Callback Function
  • 7.  php min – Find Minimum Value of Array in php
  • 8.  php max – Find Maximum Value of Array in php
  • 9.  php atanh – Find Hyperbolic Arctangent of Number Using atanh() Function
  • 10.  php cos – Find Cosine of Number in Radians Using php cos() Function

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

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

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 © 2023 · The Programming Expert · About · Privacy Policy