★ wanayoo — archive 1999 http://www.php.net/manual/function.urlencode.phpNouvelle recherche | Portail wanayoo

PHP Home Page

Manual Table of Contents
Up to URLs
Quick Reference
German version of this pageJapanese version of this pageItalian version of this pageFrench version of this pageHungarian version of this page
URLs
* base64_decode
* base64_encode
* parse_url
* rawurldecode
* rawurlencode
* urldecode
* urlencode
Manual: urlencode
View the source code for this pageSearch the site



Previous page
 urldecode
 Updated
Sat, 12 Aug 2000
Variables 
Next page


urlencode

(PHP3 , PHP4 )

urlencode -- URL-encodes string

Description

string urlencode (string str)

Returns a string in which all non-alphanumeric characters except -_. have been replaced with a percent (%) sign followed by two hex digits and spaces encoded as plus (+) signs. It is encoded the same way that the posted data from a WWW form is encoded, that is the same way as in application/x-www-form-urlencoded media type. This differs from the RFC1738 encoding (see rawurlencode()) in that for historical reasons, spaces are encoded as plus (+) signs. This function is convenient when encoding a string to be used in a query part of an URL, as a convenient way to pass variables to the next page:

Example 1. Urlencode() example


echo '<A HREF="mycgi?foo=', urlencode ($userinput), '">';
      

See also urldecode(), rawurldecode(), rawurlencode().


User Contributed Notes: urlencode


reisa@plinet.com
24-Jan-1999 05:13
Newbie Notes:



Despite it's name, <PRE>URLENCODE</PRE> and <PRE>URLDECODE<PRE> don't have to be used ONLY on URL's - you can use them to easily pass string variables, or variables that have more than one word.



For example: I have a PHP script I use throughout my website that allows visitors to "rate" and send me a short "comment" about specific pages. The comment script automatically emails me any comments written. Instead of copying the code for the "comment" script onto each page, I call the script using an <PRE>INCLUDE( )</PRE> command.



Since it is important to me to know WHAT page on my website they are commenting on, I use a variable called <PRE>$TITLE</PRE> to tell me what page they sent the comment from!



However, the title of the page is often more than one word! For example, one page has a title of "Be Wary of Scholarship Scams"



So <PRE>$TITLE="Be Wary of Scholarship Scams"</PRE>



Problem: When the comment script sends me (via email) a comment from a visitor to that page, it just says <PRE>"TITLE= Be"</PRE> It's not sending me the ENTIRE title.



So on the included comment script I put the following code:



<PRE> $title=urlencode($title);</PRE>



In the actual script that sends me the comment, I put the following code:



<PRE> $title=urldecode($title);</PRE>



Problem solved :) Title now shows in the email as <PRE>"TITLE= Be Wary of Scholarship Scams"</PRE>



To see the comment script in action, go to http://www.freschinfo.com/tipsscam.phtml Just send me email if you would like a copy of it!





danbeck@scott.net
15-Jul-1999 06:20
This function does not encode the ' character...


mkoch@ruc.dk
25-Nov-1999 05:53
I have a problem making the function work on a string including ,.

Like "Here, is a comma" should be
like "Here,+is+a+comma" - but it dosent work ?!



csnyder@mvpsoft.com
04-Jan-2000 11:46
One thing to remember when testing scripts that use urlencode() is that some browsers (I use IE5) will display the data that you have urlencoded in decoded form when you move the mouse over the link. This gave me problems for about 20 minutes before I realized what was happening...


german@chile.com
14-Jan-2000 01:08
Is there any other way to pass variables from one script to the other without having them displayed (encoded or not) on a URL query.
I need to pass variables from one script to the other, without using forms or URL querys.
please reply to the board and e-mail.



ying@zippydesign.com
23-Jan-2000 06:36
You can serialize what you want to send and store it in the hidden tag of a table, then when someone clicks the link you can submit the form. The problem with this is that you need a javascript on the client side.


ying@zippydesign.com
23-Jan-2000 06:39
Oops, I mean a hidden tag of a form, not table. Here is an example

Sending Page:

<pre>
&lt;? $stuff = urlencode(serialize($somearray)) ?>
<form name=blah method=post action=/old?u=http%3A%2F%2Fwww.php.net%2Fmanual%2F%26quot%3Bdosomething.php3%26quot%3B%26gt%3B&y=1999
<input type="hidden" name="stuff" value="&lt;? echo $stuff ?>">
</form>
<a href=/old?u=http%3A%2F%2Fwww.php.net%2Fmanual%2F%26quot%3Bjavascript%3A&y=1999 blah.submit();">Next page</a>
</pre>

Receiving Page:

&lt;?
$stuff = urldecode(unserialize($stuff));
?>



tim@falconsoft.com
14-Jul-2000 01:14
Why doesn't this delete the two cookies:

&lt;?

$Time = time() - (86400 * 2);

SetCookie("MemberID", "", $Time);
SetCookie("MemberPassword", "", $Time);

?>



tim@falconsoft.com
14-Jul-2000 01:15
Sorry about that! Wrong page!



jnich@att.com
28-Jul-2000 11:14
Is there a function that does the equivalent of perl URI::Escape's "uri_escape"? If you are using the PHP dir function to return directory entries, and you want to build a list of HREF links to the files, you need to turn spaces into %20, not +.


 About Notes


Previous page
 urldecode
 Updated
Sat, 12 Aug 2000
Variables 
Next page





Who's responsible for this?
Top of this page

Site
Hosting:



Located in
United States
Elements of this website are subject to copyright.
Questions about installing or using PHP should be directed to one of the mailing lists.
Only questions about the website should be directed to webmaster@php.net.