PHP method for processing URLs with Chinese characters

不言
Release: 2023-03-23 20:02:02
Original
3202 people have browsed it

This article mainly introduces the method of php processing with Chinese URL. It has certain reference value. Now I share it with everyone. Friends in need can refer to it.

This article mainly introduces the method of php processing. The method with Chinese URL involves operating skills related to PHP encoding conversion, which has certain reference value. Friends in need can refer to

This article describes the method of PHP processing URL with Chinese. I share it with you for your reference. The details are as follows:

ie6 There will be problems when the hyperlink has Chinese characters. It’s evil IE6. PHP’s built-in urlencode function does not work. urlencode also treats slashes and other ASCII code characters. After encoding, the problem still cannot be solved. Use the following function to solve the problem.

The principle is very simple, that is, convert all bytes larger than 127 into hexadecimal.

Chinese hyperlink address


// utf-8编码
$url = 'http://ftp.dotcoo.com/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;
}
Copy after login

Related recommendations:

php processing form upload files Method

PHP processing Excel table instance method

The above is the detailed content of PHP method for processing URLs with Chinese characters. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template