PHP supports URL encoding decoding options with the use of its core functions. PHP urlencode() and urldecode() functions are the built-in functions which usually help to perform URL encoding and decoding respectively.

Đang xem: Php url encoder

In this tutorial, we are going to see the type of URL encoding type, PHP functions to encode/decode based on the standard with examples.

Encoding is an important job in various scenarios. URL encoding helps to converts the URL by adding standard entities into it. For example, if an URL contains non-alphanumeric characters then the encoding will replace those characters with the encoding entities.

Some special characters are exceptional and will not be replaced. This will help to prevent data truncation, jumbling while accessing the URL data.

The encoding is required to be performed before sending URL data to query string or to a function which might dynamically work on this URL data.

And then, the encoded data will be decoded into its original form, after receiving it in target or landing PHP code block. Decoding is the process of reversing the encoded data back to form.

*

URL Encoding Decoding Types in PHP

In PHP, the URL encoding and decoding can be done in two different ways. Those types are listed below. Based on the content type to be encoded, choosing the encoding method will be meaningful and effective.

application/x-www-form-urlencoded typeRFC 3986 standard type

In the following sections, we will see how to apply PHP encoding decoding based on the above types of standards.

application/x-www-form-urlencoded type

For performing this type of encoding or decoding, the PHP built-in functions urlencode() and urldecode() are used. We can prefer this type when we need to send the data submitted from the form to the URL query string.

Xem thêm:

This functions will replace the special characters except (_), (-) and (.) that occurs in the given URL with %; space will be replaced with ‘+’ character.

If we send URL without encoding, then some special characters may truncate the actual data passed through the page URL. Then It will cause errors or unexpected results.

PHP Encoding Decoding Example with urlencode() urldecode()

The following example code will show how to apply encoding decoding on a URL by using PHP urlencode() urldecode() functions. In this script, the URL is assigned to a PHP variable. Then this variable will be passed to the urlencode() function.

This function will return the encoded URL. To revert this encoded URL back to its original form, the urldecode function will be used.

$url = “https://darkedeneurope.com/home.php?name=all about php”;$encodedUrl = urlencode($url);//returns http%3A%2F%2Fdarkedeneurope.com%2Fhome.php%3Fname%3Dall+about+phpecho $encodedUrl;//returns https://darkedeneurope.com/home.php?name=all about phpecho urldecode($encodedUrl);

RFC 3986 standard type

PHP functions rawurlencode() and rawurldecode() are used to encode and decode the URL with this type. By using this method the space in the URL could be replaced with % instead of plus (+) symbol. This type of encoding will be preferable when we need to create URL dynamically.

As of PHP version 5.3.0 and later, the rawurlencode uses the RFC 3986 standard. Prior to this version, it followed RFC 1738 standard.

PHP Example with rawurlencode() and rawurldecode()

This code shows how to apply the rawurlencode() and rawurldecode functions. There is no difference in using these encoding decoding functions.

Xem thêm: Học Hỏi Bí Quyết Giữ Dáng Của Sao Việt, Bí Quyết Giữ Dáng Của Sao

It will be very similar to the usage of the urlencode() urldecode() functions. The expected output is stated by using the PHP comment statement. Run this program in your PHP environment and check if you get the same output as it is in the below code.

$url = “https://darkedeneurope.com/home.php?name=all about php”;$encodedUrl = rawurlencode($url);//returns http%3A%2F%2Fdarkedeneurope.com%2Fhome.php%3Fname%3Dall%20about%20phpecho $encodedUrl;//returns https://darkedeneurope.com/home.php?name=all about phpecho rawurldecode($encodedUrl);

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Related Post

Leave a Reply

Your email address will not be published. Required fields are marked *