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.