PHP urlencode() Function Example to encode URL

Using PHP urlencode() Function Example tutorial you will learn how to encode given_url in PHP using the PHP urlencode() function with examples.

The urlencode() function is an inbuilt function in PHP which is used to encode the url. This function returns a string which consist all non-alphanumeric characters except -_. and replace by the percent (%) sign followed by two hex digits and spaces encoded as plus (+) signs.

PHP urlencode() Function

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( $url )

Example urlencode function

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

$url = "https://codingdriver.com/";
echo urlencode("$url") . "\n"; 
 
?> 

output:

https%3A%2F%2Fcodingdriver.com%2F

Example 2 : PHP URL encode Function

<?php 
 
echo urlencode("https://codingdriver.com/paytm-payment-gateway-integration-in-php.html") . "\n"; 
 echo urlencode("https://codingdriver.com/laravel-authentication-with-breeze-pacakge.html") . "\n";
?> 

Output:

https%3A%2F%2Fcodingdriver.com%2Fpaytm-payment-gateway-integration-in-php.html https%3A%2F%2Fcodingdriver.com%2Flaravel-authentication-with-breeze-pacakge.html

I hope you enjoy with this example;

Leave a Comment