ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Learn how to use PHP urlencode Function

Updated on October 24, 2011

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.

Comments

    0 of 8192 characters used
    Post Comment

    • profile image

      srinivas 

      5 years ago

      I want to how the decode this converted string

    • profile image

      Clickster 

      6 years ago

      I Hate You !

    • itech profile imageAUTHOR

      Krishna 

      7 years ago from Dausa, India

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

      Thanks for commenting.

    • Dumbledore profile image

      This Old Guy 

      7 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.

    working

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://hubpages.com/privacy-policy#gdpr

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)