PHP. supports URL encoding decoding options with the use of its core functions. PHP urlencode() và urldecode() functions are the built-in functions which usually help lớn perform URL encoding and decoding respectively.
Bạn đang xem: Php url encoder
In this tutorial, we are going to lớn see the type of URL encoding type, PHPhường. functions to lớn encode/decode based on the standard with examples.
Encoding is an important job in various scenartiện ích ios. URL encoding helps khổng lồ converts the URL by adding standard entities into lớn 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 & will not be replaced. This will help lớn prsự kiện data truncation, jumbling while accessing the URL data.
The encoding is required khổng lồ be performed before sending URL data lớn query string or lớn a function which might dynamically work on this URL data.
And then, the encoded data will be decoded into its original size, after receiving it in target or landing PHPhường code bloông chồng. Decoding is the process of reversing the encoded data baông chồng khổng lồ size.
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 nội dung type lớn be encoded, choosing the encoding method will be meaningful and effective sầu.application/x-www-form-urlencoded typeRFC 3986 standard type
In the following sections, we will see how khổng lồ apply PHPhường. encoding decoding based on the above types of standards.
For performing this type of encoding or decoding, the PHPhường built-in functions urlencode() & urldecode() are used. We can prefer this type when we need to lớn sover the data submitted from the khung to lớn the URL query string.
Xem thêm: bà đẻ ăn được trái cây gì
This functions will replace the special characters except (_), (-) và (.) that occurs in the given URL with %
If we skết thúc 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 lớn apply encoding decoding on a URL by using PHP urlencode() urldecode() functions. In this script, the URL is assigned lớn a PHPhường. variable. Then this variable will be passed khổng lồ the urlencode() function.
This function will return the encoded URL. To revert this encoded URL back to its original khung, 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+phpemang lại $encodedUrl;//returns https://darkedeneurope.com/trang chính.php?name=all about phpemang đến urldecode($encodedUrl);
RFC 3986 standard typePHPhường functions rawurlencode() and rawurldecode() are used khổng lồ encode & decode the URL with this type. By using this method the space in the URL could be replaced with %
As of PHP version 5.3.0 và later, the rawurlencode uses the RFC 3986 standard. Prior khổng lồ this version, it followed RFC 1738 standard.
PHP.. Example with rawurlencode() and rawurldecode()
This code shows how to lớn apply the rawurlencode() & rawurldecode functions. There is no difference in using these encoding decoding functions.
It will be very similar lớn the usage of the urlencode() urldecode() functions. The expected output is stated by using the PHPhường. phản hồi statement. Run this program in your PHP environment & kiểm tra if you get the same output as it is in the below code.
$url = "https://darkedeneurope.com/trang chính.php?name=all about php";$encodedUrl = rawurlencode($url);//returns http%3A%2F%2Fdarkedeneurope.com%2Fhome.php%3Fname%3Dall%20about%20phpemang lại $encodedUrl;//returns https://darkedeneurope.com/trang chính.php?name=all about phpecho rawurldecode($encodedUrl);