PHP | urlencode() Function
The "urlencode()" is one of the functions which are available in PHP by default. This function is used to encode the URL. Its working is not that complex as it looks. Instead, it is pretty simple to understand because as the output (or result), it returns a string containing all non-alphanumeric characters except (-_. ). These are replaced with the percent (%) sign followed by the two Hexa digits. To understand it more clearly lets us see its syntax in detail:
As you can see in the above-given syntax, this function accepts only a single parameter, "$input" used to hold the actual URL you want to encode.
Return Value (or output):
This function returns an encoded string upon success. Therefore the encoded or result string contains all non-alphanumeric characters except "-_." which are replaced by the "%" sign and followed by two hex digits as we have discussed above. This function encodes the URL in the same way in which data posted from the www form (or form template) is encoded.
Here we are continuously talking about URL encoding, but if you do not know anything about the URL. So it would be nice if we have some discussion about the URL to know some basics.
It is an abbreviation for Uniform Resource Locator. It can be understood as the address of a particular file or document (webpage) available on the Internet. To understand this in more detail, consider the following example:
The URL of JAVATPOINT is https://www.javatpoint.com/ and the address of this page is https://www.javatpoint.com/PHP/Urlen()". The elements used in this URL address are given below:
1. This "http://" part of the URL is considered the URL prefix that usually specifies the protocol used to access the location. There are many other URL prefixes other than this, which are as follows:
2. "www." stands for World Wide Web and is used to distinguish the content. This portion of the URL is usually not required and many times can be left out or ignored.
For example, typing "http://javatpoint.com" would still get you to the javatpoint website.
The website such as "javatpoint.com", the domain suffix is ".com" which denotes the commercial. Many other domain suffixes are also available such as ".org" which denotes the organization, "co.uk" which is for the United Kingdom and can be used as per requirements. To get yourself a domain, you can register the name through a domain registrar.
4. "PHP" and "urlen()" are the directorieswhere the web page is located on the server. Here the given web page is two directories deep. In most servers, the public_html directory is the default directory containing the HTML files.
One can also manually enter the URL by simply typing the address in the address bar(or search box) in the web browser.
For example, you might enter a website URL printed on a business card to visit the company's website. Most URLs, however, appear automatically when you click on a rel="nofollow" target="_blank"link or open a bookmark.
If the server name in the URL is not valid or that website is no more available, in that type of scenario, your browser may display a "Server not found" error. If the path in the URL is incorrect, the server may respond with a 404 error (which denotes the incorrect path).
What exactly does the urlencode () function do?
It basically performs the URL encoding in which, all the string or combinations of several character. Special characters are converted in a specific format on the basis of some predefined rules. So that these converted or encoded URL strings can be transmitted over the Internet using the ASCII character set.
However, URLs often contain characters outside the ASCII set, so the URL must be converted into a valid ASCII format. In general, the URL encoding replaces unsafe ASCII characters with a "%" followed by two hexadecimal digits, as we have discussed earlier. In addition, a URL cannot contain spaces. Therefore the urlencode() function usually replaces a space with a plus (+) sign or with %20. To understand it, let us see some examples:
Some important point related to the urlencode() function
For example -