gpt4 book ai didi

php - 如何使用 PHP 对包含 Unicode 字符的 URL 进行编码

转载 作者:可可西里 更新时间:2023-11-01 13:47:57 24 4
gpt4 key购买 nike

目前,我正在尝试寻找一种解决方案来编码包含 unicode 字符的 url,Khmer Unicode。我试过使用 php 内置函数 urlencode() 并给出了结果:例如:http://www.example.com/?kwd=Mac+Book+Pro +នៅប្រទេសយើង

虽然我使用 Google 搜索进行了测试,但结果如下: https://www.google.com.kh/#hl=en&sclient=psy-ab&q=Mac+Book+Pro+%E1%9E%93%E1%9F%85%E1%9E%94%E1%9F%92%E1%9E%9A%E1%9E%91%E1%9F%81%E1%9E%9F%E1%9E%99%E1%9E%BE%E1%9E%84&oq=Mac+Book+Pro+%E1%9E%93%E1%9F%85%E1%9E%94%E1%9F%92%E1%9E%9A%E1%9E%91%E1%9F%81%E1%9E%9F%E1%9E%99%E1%9E%BE%E1%9E%84

该怎么做?希望这里有人能帮助我。提前致谢!

最佳答案

对于 UTF-8,您可以使用:

urlencode(utf8_encode($string)); //for encoding

utf8_decode(urldecode($string)); //for decoding

对于 UTF-16,您可以使用此函数(来自 http://php.net/urlencodeurlencode 的注释):

function utf16_urlencode ( $str ) {
# convert characters > 255 into HTML entities
$convmap = array( 0xFF, 0x2FFFF, 0, 0xFFFF );
$str = mb_encode_numericentity( $str, $convmap, "UTF-8");

# escape HTML entities, so they are not urlencoded
$str = preg_replace( '/&#([0-9a-fA-F]{2,5});/i', 'mark\\1mark', $str );
$str = urlencode($str);

# now convert escaped entities into unicode url syntax
$str = preg_replace( '/mark([0-9a-fA-F]{2,5})mark/i', '%u\\1', $str );
return $str;
}

关于php - 如何使用 PHP 对包含 Unicode 字符的 URL 进行编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9901027/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com