StackTips

How to Encode and Decode URL in PHP

stacktips avtar

Written by

Editorial,  4 min read,  3.39K views, updated on Sept. 17, 2023

This post explains how to encode and decode URL using PHP. PHP supports encoding and decoding of URL by providing some built-in functions. Encoding is required before sending URL data to query string or to a function which might dynamically work on this URL data. And then, this data will be decoded into its original form, after receiving it in target PHP page or function.

PHP Encode and Decode URL Example:

First, let us create an HTML file and save it as sample.html

sample.html 

Let us now, create a PHP file to encode and decode URL.

encode_decode.php

Lets see the simple example to encode and decode URL in PHP:

$url = "https://www.example.com/p/selenium.html";
//encoding URL
$encodedUrl = urlencode($url);
echo $encodedUrl;
//Prints: https%3A%2F%2Fwww.example.com%2Fp%2Fselenium.html

//decoding URL
echo urldecode($encodedUrl);
//Prints: https://www.example.com/p/selenium.html

This all about encoding and decoding URL in PHP. Thank you for reading this article, and if you have any problem, have another better useful solution for this article, please write a message in the comment section.

This story was first published at SKPTRICKS. See the original article How to Encode and Decode URL Using PHP. Opinions expressed by Stacktips contributors are their own.

stacktips avtar

Editorial

StackTips provides programming tutorials, how-to guides and code snippets on different programming languages.

Related posts

Let’s be friends!

🙌 Stay connected with us on social media for the latest updates, exclusive content, and more. Follow us now and be part of the conversation!