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

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • 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.  Using php to Copy Files Easily from One Directory to Another
  • 2.  php unset – How to Destroy Variables in php
  • 3.  php Rename File – Renaming Files and Directories Easily
  • 4.  php sinh – Find Hyperbolic Sine of Number Using php sinh() Function
  • 5.  How to Get Session ID in php with session_id()
  • 6.  php str_split() – Convert String into Array of Equal Length Substrings
  • 7.  php array_merge() – Merge One or More Arrays Together by Appending
  • 8.  php is_string() Function – Check if Variable is String in php
  • 9.  php print_r() Function- Get Information about Variables in php
  • 10.  php array_key_exists() Function – Check if Key Exists in Array

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