Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

PHP cURL curl_unescape() Function



The PHP cURL curl_unescape() function is used to decode the given URL encoded string. It accepts two parameters and returns unescaped string on success and false on failure.

Syntax

Below is the syntax of the PHP cURL curl_unescape() function −

string curl_unescape (resource $ch, string $str)

Parameters

Below are the parameters of the curl_unescape() function −

  • $ch − It is the cURL handle returned by curl_init().

  • $str − It is the string to be unescaped.

Return Value

The curl_unescape() function returns TRUE on success and FALSE on failure.

PHP Version

First introduced in core PHP 5.5.0, the curl_unescape() function continues to function easily in PHP 7 and PHP 8.

Example 1

First we will show you the basic example of the PHP cURL curl_unescape() function to unescape a simple string.

<?php
   // Start a cURL handle
   $ch = curl_init();
   $escapedString = 'Hello%20World%21';
   $unescapedString = curl_unescape($ch, $escapedString);
   echo $unescapedString;
   curl_close($ch);
?>

Output

Here is the outcome of the following code −

Hello World!

Example 2

In the below PHP code we will use the curl_unescape() function to unescape URL parameters.

<?php
   // Start a cURL handle
   $ch = curl_init();
   $escapedUrl = 'name%3DAmit%26age%3D23';
   $unescapedUrl = curl_unescape($ch, $escapedUrl);
   echo $unescapedUrl; 
   curl_close($ch);
?> 

Output

This will generate the below output −

name=Amit&age=23

Example 3

Now the below code we will unescape a string with special characters by using the curl_unescape() function.

<?php
   // Start a cURL handle
   $ch = curl_init();
   $escapedSpecialChars = '%40%23%24%25%5E%26*%28%29';
   $unescapedSpecialChars = curl_unescape($ch, $escapedSpecialChars);
   echo $unescapedSpecialChars; 
   curl_close($ch);
?> 

Output

This will create the below output −

@#$%^&*()

Example 4

In the following example, we are using the curl_unescape() function to unescape only the path part of a URL.

<?php
   // Start a cURL handle
   $ch = curl_init();
   $escapedPath = '%2Fpath%2Fto%2Fresource';
   $unescapedPath = curl_unescape($ch, $escapedPath);
   echo $unescapedPath; 
   curl_close($ch);
?> 

Output

Following is the output of the above code −

/path/to/resource

Summary

The curl_unescape() function is a built-in function to decode the given URL encoded string. We have seen four examples which shows how curl_unescape() can be used in different scenarios to unescape strings.

php_function_reference.htm
Advertisements