Home  >  Article  >  Backend Development  >  How does php handle method code examples with Chinese URLs?

How does php handle method code examples with Chinese URLs?

怪我咯
怪我咯Original
2017-07-06 09:45:471069browse

This article mainly introduces the method of processing URLs with Chinese characters in PHP, involving operating techniques related to PHP encoding conversion, and has certain reference value. Friends in need can refer to it

The examples in this article describe PHP processing Method with Chinese URL. Share it with everyone for your reference, the details are as follows:

ie6Hyperlink There will be problems when there are Chinese characters, the evil ie6. PHP uses the built-in urlencode function No, urlencode also encodes slashes and other ASCII characters, but it still can't solve the problem. Use the following function to solve this problem.

The principle is very simple, that is, all bytes Bytes greater than 127 are converted to hexadecimal.

Chinese hyperlink address

// utf-8编码
$url = 'http://ftp.php.cn/PHP视频教程.rar';
//urlencode结果:
echo urlencode($url);
http%3A%2F%2Fftp1.zdy.co%2Fmovie%2F%E6%88%91%E4%B8%BA%E7%9B%B8%E4%BA%B2%E7%8B%82.rmvb
//link_urldecode结果:
echo link_urldecode($url);
http://ftp1.zdy.co/movie/%E6%88%91%E4%B8%BA%E7%9B%B8%E4%BA%B2%E7%8B%82.rmvb
//link_urldecode函数:
function link_urldecode($url) {
  $uri = '';
  $cs = unpack('C*', $url);
  $len = count($cs);
  for ($i=1; $i<=$len; $i++) {
    $uri .= $cs[$i] > 127 ? &#39;%&#39;.strtoupper(dechex($cs[$i])) : $url{$i-1};
  }
  return $uri;
}

The above is the detailed content of How does php handle method code examples with Chinese URLs?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn