(PHP 4, PHP 5, PHP 7, PHP 8)
urlencode — URL-kodiert einen String
Diese Funktion ist nützlich als einfache Möglichkeit, eine Zeichenkette zu kodieren, die im Abfrageteil einer URL zur Übergabe von Variablen an die nächste Seite verwendet werden soll.
string
Der zu kodierende String.
Gibt einen String zurück, in dem alle nicht-alphanumerischen Zeichen außer
-_.
durch ein Prozentzeichen (%
)
gefolgt von zwei Hexadezimalwerten und Leerzeichen durch ein Plus
(+
) ersetzt werden. Das Encoding geschieht auf dem
gleichen Wege, wie auch durch ein WWW-Formular gepostete Daten kodiert
werden - das entspricht der Auszeichnung des Dateityps
application/x-www-form-urlencoded
. Diese Auszeichnung
unterscheidet sich von der Kodierung nach » RFC 3986 (siehe auch
rawurlencode() ) dadurch, dass aus historischen Gründen
das Leerzeichen als Pluszeichen (+) kodiert wird.
Beispiel #1 urlencode()-Beispiel
<?php
$userinput = 'Data123!@-_ +';
echo "BenutzerEingabe: $userinput\n";
echo '<a href="mycgi?foo=', urlencode($userinput), '">';
?>
Das oben gezeigte Beispiel erzeugt folgende Ausgabe:
BenutzerEingabe: Data123!@-_ + <a href="mycgi?foo=Data123%21%40-_+%2B">
Beispiel #2 urlencode() und htmlentities() -Beispiel
<?php
$foo = 'Data123!@-_ +';
$bar = "Nicht derselbe Inhalt wie $foo";
echo "foo: $foo\n";
echo "bar: $bar\n";
$query_string = 'foo=' . urlencode($foo) . '&bar=' . urlencode($bar);
echo '<a href="mycgi?' . htmlentities($query_string) . '">';
?>
Das oben gezeigte Beispiel erzeugt folgende Ausgabe:
foo: Data123!@-_ + bar: Nicht derselbe Inhalt wie Data123!@-_ + <a href="mycgi?foo=Data123%21%40-_+%2B&bar=Not+the+same+content+as+Data123%21%40-_+%2B">
Hinweis:
Seien Sie vorsichtig beim Umgang mit Variablen, die HTML-Entities enthalten könnten. Angaben wie &, © und £ werden vom Browser geparst und die eigentliche Entität wird anstelle des gewünschten Variablennamens verwendet. Dies ist eine naheliegende Schwierigkeit, über die das W3C bereits seit Jahren informiert. Die entsprechende Referenz finden Sie hier: » http://www.w3.org/TR/html4/appendix/notes.html#h-B.2.2.
PHP unterstützt den Austausch des vom W3C empfohlenen Argument-Trenners Semikolon durch einen selbstgewählten Trenner über die arg_separator-Direktive in der php.ini. Leider senden die meisten User-Agents Formulardaten nicht im standardkonformen semikolongetrennten Format. Ein möglicher Weg, mit diesem Problem umzugehen, ist die Verwendung von & anstelle von & als Trennzeichen. Sie müssen dafür nicht die php.ini-Direktive arg_separator verändern. Belassen Sie sie als &, und verwenden Sie statt dessen für Ihre URLs die Funktionen htmlentities() oder htmlspecialchars() .
urlencode function and rawurlencode are mostly based on RFC 1738.
However, since 2005 the current RFC in use for URIs standard is RFC 3986.
Here is a function to encode URLs according to RFC 3986.
<?php
function myUrlEncode($string) {
$entities = array('%21', '%2A', '%27', '%28', '%29', '%3B', '%3A', '%40', '%26', '%3D', '%2B', '%24', '%2C', '%2F', '%3F', '%25', '%23', '%5B', '%5D');
$replacements = array('!', '*', "'", "(", ")", ";", ":", "@", "&", "=", "+", "$", ",", "/", "?", "%", "#", "[", "]");
return str_replace($entities, $replacements, urlencode($string));
}
?>
I needed encoding and decoding for UTF8 urls, I came up with these very simple fuctions. Hope this helps!
<?php
function url_encode($string){
return urlencode(utf8_encode($string));
}
function url_decode($string){
return utf8_decode(urldecode($string));
}
?>
this function will encode the URL while preserving the functionality of URL so you can copy and paste it in the browser
```
function urlEncode($url) {
$parsedUrl = parse_url($url);
$encodedScheme = urlencode($parsedUrl['scheme']);
$encodedHost = urlencode($parsedUrl['host']);
$encodedPath = implode('/', array_map('urlencode', explode('/', $parsedUrl['path'])));
if (isset($parsedUrl['query'])) {
$encodedQuery = '?' . urlencode($parsedUrl['query']);
} else {
$encodedQuery = '';
}
return "{$encodedScheme}://{$encodedHost}{$encodedPath}{$encodedQuery}";
}
```
Don't use urlencode() or urldecode() if the text includes an email address, as it destroys the "+" character, a perfectly valid email address character.
Unless you're certain that you won't be encoding email addresses AND you need the readability provided by the non-standard "+" usage, instead always use use rawurlencode() or rawurldecode().
I needed a function in PHP to do the same job as the complete escape function in Javascript. It took me some time not to find it. But findaly I decided to write my own code. So just to save time:
<?php
function fullescape($in)
{
$out = '';
for ($i=0;$i<strlen($in);$i++)
{
$hex = dechex(ord($in[$i]));
if ($hex=='')
$out = $out.urlencode($in[$i]);
else
$out = $out .'%'.((strlen($hex)==1) ? ('0'.strtoupper($hex)):(strtoupper($hex)));
}
$out = str_replace('+','%20',$out);
$out = str_replace('_','%5F',$out);
$out = str_replace('.','%2E',$out);
$out = str_replace('-','%2D',$out);
return $out;
}
?>
It can be fully decoded using the unscape function in Javascript.
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.