PHP urlencode() Function Example

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.

PHP – urlencode() Function

Definition: The PHP urlencode() is an inbuilt function in PHP. That is used to encode the given URL. It will return an encoded string on success.

Syntax:

The basic syntax of php urlencode() function is following:

urldecode( $given_url )

Parameters of urlencode() function

This function accepts only one parameter, that is $given_url.

Example urlencode function

Let’s take the first example of PHP encode URL. Here we have one url like “https://www.CodeOne.com”, and we will encode using the PHP urlencode() function.

<?php 
 
// PHP script to encode given url using urlencode() function 
$givenUrl = "https://www.CodeOne.com";
echo urlencode("$givenUrl") . "\n"; 
 
?> 

The output of the above PHP script is following:

https%3A%2F%2Fwww.CodeOne.com 

Example 2 of PHP encode Urls

Let’s take the second example of PHP encode URL. Here we have multiple URLs, and we will encode using the PHP urlencode() function.

<?php 
 
// PHP script to encode, given urls using urlencode function php
 
echo urlencode("https://www.CodeOne.com") . "<br>"; 
echo urlencode("https://www.CodeOne.com/word-character-counter-online-tool/") . "<br>"; 
echo urlencode("https://www.CodeOne.com/submit-tech-guest-post-write-for-us/") . "<br>"; 
 
?> 

The output of the above PHP script is the following:

 https%3A%2F%2Fwww.CodeOne.com
 https%3A%2F%2Fwww.CodeOne.com%2Fword-character-counter-online-tool%2F
 https%3A%2F%2Fwww.CodeOne.com%2Fsubmit-tech-guest-post-write-for-us%2F

Leave a Reply

Your email address will not be published.