Re: Best way to encode?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



in HTML with HTTP_GET, you can use:
xxx.href = "my.php?data="+encodeURI(escape("...foreign strings..."));

my.php:

$data = uniDecode($_GET["data"]);
echo $data;



function uniDecode($sText) {
  $sData = preg_replace_callback("/%u[0-9A-Za-z]{4}/",toUtf8, $sText);
  return unescape($sData);
}

function toUtf8($ar) {
  $c = "";
  foreach($ar as $val) {
    $val = intval(substr($val,2),16);
    if($val < 0x7F){        // 0000-007F
      $c .= chr($val);
    }
    elseif ($val < 0x800) { // 0080-0800
      $c .= chr(0xC0 | ($val / 64));
      $c .= chr(0x80 | ($val % 64));
    }
    else {                  // 0800-FFFF
      $c .= chr(0xE0 | (($val / 64) / 64));
      $c .= chr(0x80 | (($val / 64) % 64));
      $c .= chr(0x80 | ($val % 64));
    }
  }
  return $c;
}

function unescape($sText) {
  $sTranArray = array("%09" => "\t",
                      "%0A" => "\n",
                      "%0B" => "\x0b",
                      "%0D" => "\r",
                      "%20" => " ",
                      "%21" => "!",
                      "%22" => "\"",
                      "%23" => "#",
                      "%24" => "$",
                      "%25" => "%",
                      "%26" => "&",
                      "%27" => "'",
                      "%28" => "(",
                      "%29" => ")",
                      "%2C" => ",",
                      "%3A" => ":",
                      "%3B" => ";",
                      "%3C" => "<",
                      "%3D" => "=",
                      "%3E" => ">",
                      "%3F" => "?",
                      "%5C" => "\\",
                      "%5B" => "[",
                      "%5D" => "]",
                      "%5E" => "^",
                      "%60" => "`",
                      "%7C" => "|",
                      "%7B" => "{",
                      "%7D" => "}",
                      "%7E" => "~");
  return strtr($sText, $sTranArray);
}

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux