Web студия "GrandView"
  Главная   Написать Контакты
   
   
О проекте
Руководство php
 

urlencode

(PHP 3, PHP 4, PHP 5)

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 a URL, as a convenient way to pass variables to the next page:

Пример 1. urlencode() example

<?php
echo '<a href="mycgi?foo=', urlencode($userinput), '">';
?>

Note: Be careful about variables that may match HTML entities. Things like &amp, &copy and &pound are parsed by the browser and the actual entity is used instead of the desired variable name. This is an obvious hassle that the W3C has been telling people about for years. The reference is here: http://www.w3.org/TR/html4/appendix/notes.html#h-B.2.2. PHP supports changing the argument separator to the W3C-suggested semi-colon through the arg_separator .ini directive. Unfortunately most user agents do not send form data in this semi-colon separated format. A more portable way around this is to use &amp; instead of & as the separator. You don't need to change PHP's arg_separator for this. Leave it as &, but simply encode your URLs using htmlentities() or htmlspecialchars().

Пример 2. urlencode() and htmlentities() example

<?php
$query_string
= 'foo=' . urlencode($foo) . '&bar=' . urlencode($bar);
echo
'<a href="mycgi?' . htmlentities($query_string) . '">';
?>

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



Variables handling> <urldecode
Last updated: Sat, 27 Jan 2007
 
add a note add a note User Contributed Notes
urlencode
Thomas
05-Sep-2007 02:30
Reply to 'peter at mailinator dot com'

If you are having problems using urldecode in PHP following the escape() function in Javascript, try to do a decodeURI() before the escape(). This fixed it for me at least.

Thomas
Peter
31-Aug-2007 05:48
Try this one out live:
urlencode-online.com
mmj48.com
07-Aug-2007 08:28
What I use instead:

<?php
function escape($url)
{
    return
str_replace("%2F", "/", urlencode($url));
}
?>
ahrensberg at gmail dot com
04-Aug-2007 05:04
Like "Benjamin dot Bruno at web dot de" earlier has writen, you can have problems with encode strings with special characters to flash. Benjamin write that:

<?php
  
function flash_encode ($input)
   {
      return
rawurlencode(utf8_encode($input));
   }
?>

... could do the problem. Unfortunately flash still have problems with read some quotations, but with this one:

<?php
  
function flash_encode($string)
   {
     
$string = rawurlencode(utf8_encode($string));

     
$string = str_replace("%C2%96", "-", $string);
     
$string = str_replace("%C2%91", "%27", $string);
     
$string = str_replace("%C2%92", "%27", $string);
     
$string = str_replace("%C2%82", "%27", $string);
     
$string = str_replace("%C2%93", "%22", $string);
     
$string = str_replace("%C2%94", "%22", $string);
     
$string = str_replace("%C2%84", "%22", $string);
     
$string = str_replace("%C2%8B", "%C2%AB", $string);
     
$string = str_replace("%C2%9B", "%C2%BB", $string);

      return
$string;
   }
?>

... should solve this problem.
mensonge
26-Jul-2007 06:29
I had difficulties with all above solutions. So I applied a dirty simple solution by using:

base64_encode($param)
and
base64_decode($param)

The string's length is a bit longer but no more problem with encoding.
sebastiaan at netomzet dot nl
05-Mar-2007 09:04
quote: "Apache's mod_rewrite and mod_proxy are unable to handle urlencoded URLs properly - http://issues.apache.org/bugzilla/show_bug.cgi?id=34602"

The most simple solution is to use urlencode twice!

echo urlencode(urlencode($var));

Apache's mod_rewrite will handle it like a normal string using urlencode once.
in reply to "kL"
20-Feb-2007 01:23
kL's example is very bugged since it loops itself and the encode function is two-way.

Why do you replace all %27 through '  in the same string in that you replace all ' through %27?

Lets say I have a string: Hello %27World%27. It's a nice day.
I get: Hello Hello 'World'. It%27s a nice day.

With other words that solution is pretty useless.

Solution:
Just replace ' through %27 when encoding
Just replace %27 through ' when decoding. Or just use url_decode.
rm<at>mindbench<dot>nl
14-Feb-2007 05:32
Another thing to keep in mind is that urlencode is not unicode.

For example, urlencoding enqu
Новости
11 июля 2007
Сайт запущен
© 2007 info@grandviewstudio.com
Z058440144362 Z348613067571