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.

  1. "urlencode()" function of PHP,
  2. 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.

  1. Description and Usage of both functions,
  2. Syntax of both function,
  3. Parameters details,
  4. Conversion Table of All ASCII Characters into Url Encoded format,
  5. Practical Examples.
  6. Ask for Help.

PHP Urlencode functions
PHP Urlencode functions

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.

More by this Author


Comments 4 comments

Dumbledore profile image

Dumbledore 5 years ago from Somewhere in Ohio

I have often wandered what was meant by the statement "url encoded" when I viewed certain packet connections. I thought that the URLs were encrypted. You have brightened my horizons. Thank you for this informative hub.


itech profile image

itech 5 years ago from New Delhi, India Author

Glad to see that, My Tutorial Helped you in clarifying your doubts.

Thanks for commenting.


Clickster 4 years ago

I Hate You !


srinivas 3 years ago

I want to how the decode this converted string

    Sign in or sign up and post using a HubPages Network account.

    0 of 8192 characters used
    Post Comment

    No HTML is allowed in comments, but URLs will be hyperlinked. Comments are not for promoting your articles or other sites.


    Click to Rate This Article
    working