如何编码包含Unicode的网址?我想将它传递给命令行实用程序,我需要先对其进行编码。
<form action="index.php" method="get" >
<input type="text" name="find" value="عربي" /><br />
<input type="submit" value="search" />
<form />
实施例: http://localhost/index.php?find =عربي
答案 0 :(得分:2)
$ cat testme
#!/usr/local/bin/php
<?php
$chars = array( "d8", "b9", "d8", "b1", "d8", "a8", "d9", "8a" );
foreach ($chars as $one) {
$string .= sprintf("%c", hexdec($one));
}
print "String: " . $string . "\n";
print "Encoded: " . urlencode($string) . "\n";
?>
$ ./testme
String: عربي
Encoded: %D8%B9%D8%B1%D8%A8%D9%8A
$
答案 1 :(得分:1)
%DA%D1%C8%ED
不是Unicode序列,而是其他编码中的URL编码序列。
URL编码后的Unicode中的عربي
应变为%D8%B9%D8%B1%D8%A8%D9%8A
如果要构建具有Unicode字符的有效URL,可以使用以下内容:
$url = 'http://localhost/index.php?find=عربي';
$url_parts = parse_url($url);
$query_parts = array();
parse_str($url_parts['query'], $query_parts);
$query = http_build_query(array_map('rawurlencode', $query_parts));
$url_parts['query'] = $query;
$encoded_url = http_build_url($url_parts);
注意:这只会编码网址的query
部分。