★ wanayoo — archive 1999 http://uk.php.net/manual/cs/function.urlencode.phpNouvelle recherche | Portail wanayoo
PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<urldecodeVariables>
view the version of this page
Last updated: Thu, 08 Jan 2004

urlencode

(PHP 3, PHP 4 )

urlencode -- URL-kódovat řetězec

Popis

string urlencode ( string str)

Vrátí řetězec, ve kterém byly všechny nealfanumerické znaky kromě -_. nahrazeny znakem procent (%) následovaným dvěma šestnácktovými číslicemi a mezery kódovány jako znaky plus (+). Kódování je stejné jako u dat postovaných z WWW formuláře, tj. stejně jako u application/x-www-form-urlencoded typu. To se liší od RFC1738 kódování (viz rawurlencode()) v tom, že z historických důvodů se mezery kódují jako znaky plus (+). Tato funkce je vhodná při kódování řetězce, který se má použít jako query část URL jako příhodný způsob předání proměnných na další stránku:

Příklad 1. Příklad urlencode()

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

Poznámka: pozor při předávání proměnných, které by mohly odpovídat HTML entitám. Věci jako &amp, &copy a &pound browser analyzuje a místo požadovaného jména proměnné použije odpovídající entitu. To je zřejmý problém, na který W3C upozorňuje už léta. Příručka je tady: http://www.w3.org/TR/html4/appendix/notes.html#h-B.2.2 PHP podporuje změnu oddělovače argumentů na středník doporučovaný W3C skrze .ini direktivu arg_separator. Bohužel, většina uživatelských programů neposílá data z formulářů v tomto formátu. Přenositelnější formou je použít jako oddělovač &amp; místo &. Na to nemusíte měnit arg_separator. Nechte ho na &, ale kódujte URL pomocí htmlentities() (urlencode($data)).

Příklad 2. Příklad na urlencode/htmlentities()

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

Viz také urldecode(), htmlentities(), rawurldecode(), rawurlencode().



add a note add a note User Contributed Notes
urlencode
alexanderangelov at abv dot bg
18-Feb-2003 09:53
When you're filling in a form sometimes you might forget some of the
required fields and when you go back everything is empty
and you have to fill in all of the fields again. To avoid this you can
pass the values of the filled fields to the same page. But some of the
values of the variables can contain spaces, names for example:'John Smith'.
So i use str_replace() instead of urlencode() to replace spaces with '+'
and 'John Smith' turns into 'John+Smith'. Here is the example:
<?php
function get_rid_of_spaces_and_empty_values($name, $city, $mail, $phone) {
$href_link = "your_form.php";
if (
$name) {
$name = str_replace(" ", "+", $name);
$href_link .= "?name=$name";
}
if (
$city) {
$city = str_replace(" ", "+", $city);
$href_link .= $href_link == "your_form.php" ? "?city=$city" : "&city=$city";
}
if (
$mail) {
$mail = str_replace(" ", "+", $mail);
$href_link .= $href_link == "your_form.php" ? "?mail=$mail" : "&mail=$mail";
}
if (
$phone) {
$phone = str_replace(" ", "+", $phone);
$href_link .= $href_link == "your_form.php" ? "?phone=$phone" : "&phone=$phone";
}
return
$href_link;
}   
/*If there is no value for $name and $city == city_value,
$href_link will be "your_form.php?city=city_value";
else it will be "your_form.php?name=name_value&city=city_value" etc.*/ 
function check_all_inputs_in_the_form($name, $city, $mail, $phone) {
  
$href_link = get_rid_of_spaces_and_empty_values($name, $city, $mail, $phone);
if ( (!
$name)||(!$city)||(!$mail)||(!$phone) ) {
 print(
" <html><head><title>form</title></head>\n ".
      
" <body>Please, go <a href=/old?u=http%3A%2F%2Fuk.php.net%2Fmanual%2Fcs%2F%3C%2Fspan&y=1999>\"$href_link\">back</a> and fill in all required fields.\n ".
      
" </body></html>\n " );   
         exit;
     }   
}
if (!
$hid) {
?>
<html><head><title>form</title></head>
<body><form name="something" method="post" action="/old?u=http%3A%2F%2Fuk.php.net%2Fmanual%2Fcs%2Fyour_form.php&y=1999">
name:<input type="text" name="name" <?php if ($name) print( "value=\"$name\"" ); ?>>
e-mail:<input type="text" name="mail" <?php if ($mail) print( "value=\"$mail\"" ); ?>>
phone:<input type="text" name="phone" <?php if ($phone) print( "value=\"$phone\"" ); ?>>
city:<input type="text" name="city" <?php if ($city) print( "value=\"$city\"" ); ?>>
<input type="hidden" name="hid" value="hid">
<input type="submit" value="submit" name="submit"></form></body></html>
<?php
}
if (
$hid == "hid") check_all_inputs_in_the_form($name, $city, $mail, $phone);
?>
I hope this will be helpful :)
leckermatz attt yahoo dottt de
05-Nov-2002 06:16
Addendum: In Flash 6 there finally is a working method of passing special characters into a flash movie.In order to do this, the php file which generaes the output (an is adressen in flash via the "loadVariables" action) must be saved as an UTF-8 encoded file. By doing so, the file will gain an BOM and will be properly recognized as UTF8-encoded by the flash movie.

Now all you need to do is something like
echo "textvar=".urlencode(utf8_encode("lots of special chars: äöü..."));
and that should work fine.

another thing:
Flash interprets both \r and \n as newline commands. Therefore ot will insert 2 line breaks whenever there is only one. i.e., nl2br($string) will NOT work properly. Better use a function that will replace "\n" values from your database with "<br />" rather than "<br />\r", as nl2br would do. Example:

function nltobr($input)
  {
  $input=nl2br($input);
   $input=str_replace("\r","",$input);
  return $input;
  }
leckermatz attt yahoo dotttt de
23-Oct-2002 04:40
Whilst using Flash and a mysql databasses with php as the connecting interface, i stumbled across some problems with the display of german special characters (such as ß or ä,ö,ü). Seemingly, even though te flash manual wans you to completely urlencode data send to the flash movie, those characters will only be displayed correctly if transmitted _UN_encoded.
issue9mm at leafapplication dot com
07-Oct-2002 11:53
Just a simple comment, really, but if you need to encode apostrophes, you should be using rawurlencode as opposed to just urlencode.

Naturally, I figured that out the hard way.
chris at cwcomputerservices dot co dot uk
06-Sep-2002 11:22
To encode a '+' (plus) symbol so it ends up as '+' when decoded automatically by PHP, you have to do this:

rawurlencode(rawurlencode("+"));

If you just call rawurlencode() once, the resulting "%2B" is converted to '+' before '+' symbols are converted to spaces, which is not very useful.
Webmaster[AT]ReikiMagazine[DOT]com
15-Jul-2002 08:37
Many examples here on how to convert spaces, but how about converting linefeeds or carriage returns:

I needed a form that also contained a textarea field to add notes if applicable. I used a require() function for including the form again with all of its data when an error occured, e.g. a required field was not filled in. This presented a include failure when a return was used in the notes-field. I had allready translated spaces to + with the str_replace() function, but I couldn't solve this one. I finally used this trick:

// just before including the form and passing on the values
$notes = urlencode($notes);
$notes = str_replace("%0A","[LF]",$notes); // linefeed converted to "[LF]"
$notes = str_replace("%0D","[CR]",$notes); // carriage return converted to "[CR]"
$notes = urldecode($notes);

And now in the form include:
// just before printing the textarea:
$notes = str_replace("[LF]","\n",$notes); // "[LF]" converted back
$notes = str_replace("[CR]","\r",$notes); // "[CR]" converted back

I couldn't find any other way, maybe this is of any help to anyone here. I just assume nobody ever types [LF] or [CR] in the notes-field.
sipsick at xxl dot ee
05-Mar-2002 10:38
I have tried to make a redirection with urlencode(). If the url contains "/" characters, they are replaced with "%2F" and browser won't find the file.  This is expected behavior, just a warning to people.
jeremy at nirvani dot net
30-Jan-2002 05:18
REGARDING FILES WITH SPACES -- On some server setups, Apache & php (4.0 and/or 4.1) do not like urlencoded URLS for files that have spaces. For example 'File with spaces in it.html'. They should, but do not.  If your server is set up in a way which does not properly handle a request for a URL with a file with spaces, like: http://www.planet.mars/File+with+spaces+in+it.html then instead of using 'urlencode' to encode the filename, use 'rawurlencode' to encode the file name instead. 'rawurlencode' will render the file name of the URL to be File%20with%20spaces%20in%20it.html  Notice, the difference being that the spaces in the file name are instead of '+' (urlencode) turned into '%20' (rawurlencode).
matt at sekelsky dot com
02-Jan-2002 07:25
In netscape 4.x url's are not automatically encoded. If you are passing information to javascript and then using document.location.replace(url) to switch the page, go ahead and put urlencode() around your php vars that are being passed. It will help encode the url properly for netscape users.
martin at humany dot com
11-Oct-2001 02:41
Be aware that WML post data uses %20 instead of spaces.
st dot n at gmx dot net
16-Nov-2000 03:12
The documentation above suggests using htmlentities() to quote the
ampersand character.  This is overkill and has the problems stated in the
comments to htmlentities() that this function supports only ISO-8859-1.
I think it should be enough to use htmlspecialchars() for that purpose.
durchholz at sourceforge dot net
25-Aug-2000 08:46
If you need to turn spaces into %20, not +, see rawurlencode().

<urldecodeVariables>
 Last updated: Thu, 08 Jan 2004
show source | credits | sitemap | contact | mirror sites 
Copyright © 2001-2004 The PHP Group
All rights reserved.
This mirror generously provided by: Kewlio.net Limited
Last updated: Wed Jan 28 23:14:58 2004 GMT