PHP : How to handle URI or URL with non-ASCII characters such as Chinese/Japanese/Korean(CJK) ?
This tutorial is for CodeIgniter, but it should work with any PHP framework or raw PHP.
While working with CodeIgniter, I need to grab URI with Chinese characters such as :
http://webserver.com/令吉大贬伤内需‧白文春-0fu15fw
with the CodeIgniter's URI helper.
$query = $this->uri->segment(2);
and use the $query as a parameter to search my database and pull out the relevent data.
Trouble is, the $query will become
%E4%BB%A4%E5%90%89%E5%A4%A7%E8%B4%AC%E4%BC%A4%E5%86%85%E9%9C%80%E2%80%A7%E7%99%BD%E6%96%87%E6%98%A5-0fu15fw
instead of 令吉大贬伤内需‧白文春-0fu15fw. Because of not having the URL decoded properly, the database query will not be correct and will always return empty result.
To fix this problem, use the PHP urldecode()
function. For example
$query = urldecode($this->uri->segment(2));
This will ensure the non-ASCII characters get in the URL decoded properly. Hope this tutorial can be useful to you.
References :
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+10.3k RPM : error: db3 error(-30974) from dbenv->failchk: DB_RUNRECOVERY: Fatal error, run database recovery
+36.4k Golang : Display float in 2 decimal points and rounding up or down
+5.6k Golang : Launching your executable inside a console under Linux
+11.5k How to tell if a binary(executable) file or web application is built with Golang?
+10.6k Golang : Removes punctuation or defined delimiter from the user's input
+14.2k How to automatically restart your crashed Golang server
+21.8k Golang : Match strings by wildcard patterns with filepath.Match() function
+13.6k Golang : Human readable time elapsed format such as 5 days ago
+33.7k Golang : Proper way to set function argument default value
+9.1k Golang : How to get ECDSA curve and parameters data?
+13.4k Golang : Set image canvas or background to transparent
+5.4k Golang : Detect words using using consecutive letters in a given string