Tutorials PHP How to Encode and Decode URL in PHP

How to Encode and Decode URL in PHP

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



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


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.

Inline Feedbacks
View all comments
Previous Post

Default Methods for Interfaces in Java8

Next Post

Using H2 In-Memory Database in Spring Boot

Related Posts
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All
Would love your thoughts, please comment.x