- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming»
- Programming Languages
Learn how to use PHP urlencode Function
Introduction
If you ever found yourself being stuck in a condition where you want to encode a URL or string in order to pass parameters/variables and their values from one PHP page to another, which are playing a significant role in development of your web application. Then, PHP has provided an inbuilt solution of it with two type of functions from it's large Library of functions, which are shown below.
- "urlencode()" function of PHP,
- And "rawurlencode()" function of PHP.
In this PHP tutorial, I'm Publishing some information about both of these two functions. You can click on anchor links to jump directly on page section.
- Description and Usage of both functions,
- Syntax of both function,
- Parameters details,
- Conversion Table of All ASCII Characters into Url Encoded format,
- Practical Examples.
- Ask for Help.
Description and Usage
Urlencode PHP Function
- This PHP function encodes the parameter values of URL by replacing non-alphanumeric characters with some predefined hex digits with a "%" sign before them.
Rawurlencode PHP Function
-
This PHP function also does the same task but this time it encodes the given string in accordance to RFC 3986 (Requests for Comments).
Syntax
There is also no big difference in syntax's of both functions, which you can see below.
//-----Below is the Syntax of urlencode function----- string urlencode ( string $str ); //--------------------------------------------------- //-----And, here is syntax of rawurlencode function-- string rawurlencode ( string $str );
Parameters Details
Both of these functions accepts only one parameter which should be of string data type.
- "$str" - The String or URL on which encoding is to be performed.
Output
- Both of this functions output an encoded string/URL of Original Value as clarified in Usage section above.
Conversion Table : ASCII Characters into Url Encoded form
ASCII Character URL-encoding <!-----------------------------------------> space %20 ! %21 " %22 # %23 $ %24 % %25 & %26 ' %27 ( %28 ) %29 * %2A + %2B , %2C - %2D . %2E / %2F 0 %30 1 %31 2 %32 3 %33 4 %34 5 %35 6 %36 7 %37 8 %38 9 %39 : %3A ; %3B < %3C = %3D > %3E ? %3F @ %40 A %41 B %42 C %43 D %44 E %45 F %46 G %47 H %48 I %49 J %4A K %4B L %4C M %4D N %4E O %4F P %50 Q %51 R %52 S %53 T %54 U %55 V %56 W %57 X %58 Y %59 Z %5A [ %5B \ %5C ] %5D ^ %5E _ %5F ` %60 a %61 b %62 c %63 d %64 e %65 f %66 g %67 h %68 i %69 j %6A k %6B l %6C m %6D n %6E o %6F p %70 q %71 r %72 s %73 t %74 u %75 v %76 w %77 x %78 y %79 z %7A { %7B | %7C } %7D ~ %7E %7F ⬠%80 %81 â %82 Æ %83 â %84 ⦠%85 â %86 â¡ %87 Ë %88 â° %89 Å %8A â¹ %8B Å %8C %8D Ž %8E %8F %90 â %91 â %92 â %93 â %94 ⢠%95 â %96 â %97 Ë %98 ⢠%99 Å¡ %9A ⺠%9B Å %9C %9D ž %9E Ÿ %9F %A0 ¡ %A1 ¢ %A2 £ %A3 %A4 Â¥ %A5 | %A6 § %A7 ¨ %A8 © %A9 ª %AA « %AB ¬ %AC ¯ %AD ® %AE ¯ %AF ° %B0 ± %B1 ² %B2 ³ %B3 ´ %B4 µ %B5 ¶ %B6 · %B7 ¸ %B8 ¹ %B9 º %BA » %BB ¼ %BC ½ %BD ¾ %BE ¿ %BF à %C0 à %C1 à %C2 à %C3 à %C4 à %C5 à %C6 à %C7 à %C8 à %C9 à %CA à %CB à %CC à %CD à %CE à %CF à %D0 à %D1 à %D2 à %D3 à %D4 à %D5 à %D6 %D7 à %D8 à %D9 à %DA à %DB à %DC à %DD à %DE à %DF à %E0 á %E1 â %E2 ã %E3 ä %E4 Ã¥ %E5 æ %E6 ç %E7 è %E8 é %E9 ê %EA ë %EB ì %EC à %ED î %EE ï %EF ð %F0 ñ %F1 ò %F2 ó %F3 ô %F4 õ %F5 ö %F6 ÷ %F7 ø %F8 ù %F9 ú %FA û %FB ü %FC ý %FD þ %FE ÿ %FF
Practical Examples
Here, below are some examples that will help you in understanding the usage of this function.
Example 1 : Usage of urlencode PHP Function
<?php $paramVALUE='|---l0\/[+---$$$'; echo '<a href="http://example.com/test.php?parameter=' .urlencode($paramVALUE) .'">http://example.com/test.php?parameter=' .urlencode($paramVALUE) .'</a>' ; //This PHP Statement Will Output result shown below /* http://example.com/test.php?parameter=%7C---l0%5C%2F%5B%2B---%24%24%24 */ ?>
Example 2 : Usage of rawurlencode PHP Function
<?php echo 'http://example.com/email/'.rawurlencode('johny$-D-~Bravo@talent.com/'); //This PHP statement will output the result shown below /* http://example.com/email/johny%24-D-~Bravo%40talent.com%2F */ ?>
Note
In the second example, "~" (tilde) sign is not converted into URL encoded form when using "rawurlencoded" function of PHP.
Ask for Help
If you have any queries regarding this function or about any part of this tutorial, then, You are free to ask about it in comments section below.