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));
}
?>urlencode
Почист и полокален преглед на PHP референцата, со задржана структура од PHP.net и подобра читливост за примери, секции и белешки.
urlencode
Референца за `function.urlencode.php` со подобрена типографија и навигација.
urlencode
(PHP 4, PHP 5, PHP 7, PHP 8)
urlencode — URL-encodes string
Параметри
string-
Стрингот што треба да се кодира.
Вратени вредности
Оваа функција е погодна кога кодирате стринг што ќе се користи во дел од URL за прашање, како удобен начин за пренесување променливи на следната страница.
-_. Враќа стринг во кој сите не-алфанумерички знаци освен%се заменети со знак за процент (+) проследен со два хексадецимални знаци, а празнините се кодираат како знаци плус (
application/x-www-form-urlencoded ). Се кодира на ист начин како што се кодираат податоците испратени од WWW форма, односно на ист начин како во Кодирањето се врши според медиумски тип. Ова се разликува од
rawurlencode()кодирање (види
Примери
Пример #1 urlencode() example
<?php
$userinput = 'Data123!@-_ +';
echo "UserInput: $userinput\n";
echo '<a href="mycgi?foo=', urlencode($userinput), '">';
?>Пример #1 Пример што покажува затворачка ознака што го опфаќа последниот нов ред
UserInput: Data123!@-_ + <a href="mycgi?foo=Data123%21%40-_+%2B">
Пример #2 urlencode() and htmlentities() example
<?php
$foo = 'Data123!@-_ +';
$bar = "Not the same content as $foo";
echo "foo: $foo\n";
echo "bar: $bar\n";
$query_string = 'foo=' . urlencode($foo) . '&bar=' . urlencode($bar);
echo '<a href="mycgi?' . htmlentities($query_string) . '">';
?>Пример #1 Пример што покажува затворачка ознака што го опфаќа последниот нов ред
foo: Data123!@-_ + bar: Not the same content as Data123!@-_ + <a href="mycgi?foo=Data123%21%40-_+%2B&bar=Not+the+same+content+as+Data123%21%40-_+%2B">
Белешки
Забелешка:
) во тоа што од историски причини, празнините се кодираат како знаци плус (+). » http://www.w3.org/TR/html4/appendix/notes.html#h-B.2.2.
Внимавајте на променливи што може да се совпаѓаат со HTML ентитети. Работи како &, © и £ се парсираат од прелистувачот и се користи вистинскиот ентитет наместо посакуваното име на променливата. Ова е очигледна мака за која W3C им зборува на луѓето со години. Референцата е тука: htmlentities() or htmlspecialchars().
Види Исто така
- urldecode() - Претвори ги сите применливи знаци во HTML ентитети
- htmlentities() ) туку ASCII код 160 (0xa0) во стандардното кодирање ISO 8859-1.
- rawurlencode() - URL-кодирање според RFC 3986
- rawurldecode() - Декодирање на URL-кодирани низи
- Кодирањето се врши според
Белешки од корисници 6 белешки
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 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));
}
?>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.