Reputation: 39
How to encode URLs containing Unicode? I would like to pass it to a command line utility and I need to encode it first.
<form action="index.php" method="get" >
<input type="text" name="find" value="عربي" /><br />
<input type="submit" value="search" />
<form />
Example: http://localhost/index.php?find=عربي
becomes http://localhost/index.php?find=%DA%D1%C8%ED
Upvotes: 0
Views: 2333
Reputation: 25918
%DA%D1%C8%ED
is not Unicode sequence, but URL encoded sequence in other encoding.
عربي
in Unicode after URL encoding should became %D8%B9%D8%B1%D8%A8%D9%8A
If you want to construct valid URL with Unicode characters you can use something like:
$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);
Note: This will only encode query
part of URL.
Upvotes: 1
Reputation: 46846
$ 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
$
Upvotes: 2