Last active
February 25, 2020 23:03
-
-
Save dertajora/0138995354b1c963515924928ddf517c to your computer and use it in GitHub Desktop.
Decode Encoded Unicode string (field html-instruction) on Google Maps API response
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Lets say , we want to send a direction request with guidance to Google Maps API using link below. | |
// https://maps.googleapis.com/maps/api/directions/json?origin=-6.190109,%20106.798565&destination=-6.177596,%20106.792611&key=USEYOUR_KEY | |
// some of request, especially in field html-instruction would return unicode html text like this one ('Head \u003cb\u003ewest\u003c/b\u003e'). | |
// for some reason, I just want to use the text provided there ('Head west'). | |
// after some googling and asking, here is the best code I have (right now) | |
# example response | |
//Type A : There are 2 sentence in one single direction response | |
$str = "Turn \u003cb\u003eleft\u003c/b\u003e to stay on \u003cb\u003eJl. Letjen S. Parman\u003c/b\u003e\u003cdiv style=\"font-size:0.9em\"\u003ePass by Slipi Kemanggisan (on the right in 1.2 km)\u003c/div\u003e"; | |
//Type B : There are 1 sentence in one single direction response | |
$str = "Head \u003cb\u003ewest\u003c/b\u003e"; | |
// use to delete all unicode string found on str variable, unicode is detected by \uxxxx | |
$new_str = preg_replace_callback('/\\\\u([0-9a-fA-F]{4})/', function ($match) { | |
return mb_convert_encoding(pack('H*', $match[1]), 'UTF-8', 'UCS-2BE'); | |
}, $str); | |
// when there are new line in sentence using div, we will insert ". " string before it | |
$prefix = ". "; | |
$new_str = substr_replace($new_str, $prefix, strpos($new_str, "<div"), 0); | |
// sometimes the result of previous code is like this : | |
// A. ". Head west" | |
// B. "Turn left to stay on Jl. Letjen S. Parman. Pass by Slipi Kemanggisan (on the right in 1.2 km)" | |
// then if it turns out the sentence response only 1 (like described in example A), we will just delete the prefix using code below | |
if (substr($new_str, 0, strlen($prefix)) == $prefix) { | |
$new_str = substr($new_str, strlen($prefix)); | |
} | |
// remove all html tags | |
$direction = strip_tags($new_str); | |
echo $direction; | |
#credit | |
#https://stackoverflow.com/questions/2934563/how-to-decode-unicode-escape-sequences-like-u00ed-to-proper-utf-8-encoded-cha |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment