0

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

5
  • drupalcode.com/api/function/mb_urlencode/contrib-5.x-1.x Commented Jan 30, 2012 at 14:45
  • So you want to عربي as a parameter value but don’t know how to convert it into %DA%D1%C8%ED, right? Commented Jan 30, 2012 at 14:45
  • Why do you not use POST? POST request will encode properly. Commented Jan 30, 2012 at 14:46
  • @e-zinc So you want to tell anyone who doesn't use a language fully described by the ASCII charset that they just shouldn't use GET? Commented Jan 30, 2012 at 14:51
  • @rdlowrey I want only say that the POST usage will be easier if POST is allowed by server side. Commented Jan 30, 2012 at 14:58

2 Answers 2

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
$ 
Sign up to request clarification or add additional context in comments.

Comments

1

%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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.