首頁  >  文章  >  後端開發  >  PHP如何對中文進行轉義?

PHP如何對中文進行轉義?

Guanhui
Guanhui原創
2020-07-23 09:11:412984瀏覽

PHP如何對中文進行轉義?

PHP如何將中文轉義?

在PHP中可以透過使用“urlencode()”函數將中文進行轉義,該函數的作用是對URL字串進行編碼,其語法為“urlencode($str )”,使用時將中文傳入即可。

使用範例

<?php
echo &#39;<a href="mycgi?foo=&#39;, urlencode($userinput), &#39;">&#39;;
?>
<?php
function fullescape($in)
{
  $out = &#39;&#39;;
  for ($i=0;$i<strlen($in);$i++)
  {
    $hex = dechex(ord($in[$i]));
    if ($hex==&#39;&#39;)
       $out = $out.urlencode($in[$i]);
    else
       $out = $out .&#39;%&#39;.((strlen($hex)==1) ? (&#39;0&#39;.strtoupper($hex)):(strtoupper($hex)));
  }
  $out = str_replace(&#39;+&#39;,&#39;%20&#39;,$out);
  $out = str_replace(&#39;_&#39;,&#39;%5F&#39;,$out);
  $out = str_replace(&#39;.&#39;,&#39;%2E&#39;,$out);
  $out = str_replace(&#39;-&#39;,&#39;%2D&#39;,$out);
  return $out;
}
?>
<?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;
   }
?>

推薦教學:《PHP

以上是PHP如何對中文進行轉義?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn