0

I have a GPS which returns address in a UNICODE string format, for example:

0053 004F 0053 0028 004C 0029 003A 0053 0068 0069 006D 0069 006E 0020 0046 0061 0069 
0072 0079 006C 0061 006E 0064 0020 0057 0065 0073 0074 0020 0052 0064 002C 0048 0075 
0069 0063 0068 0065 006E 0067 002C 0048 0075 0069 007A 0068 006F 0075 002C 0047 0075 
0061 006E 0067 0064 006F 006E 0067 0028 004E 0032 0033 002E 0031 0031 0031 002C 0045 
0031 0031 0034 002E 0034 0031 0031 0029 004E 0065 0061 0072 0062 0079

should be (according to this website: http://rishida.net/tools/conversion/)

S O S ( L ) : S h i m i n   F a i r y l a n d   W e s t   R d , 
H u i c h e n g , H u i z h o u , 
G u a n g d o n g ( N 2 3 . 1 1 1 , E 1 1 4 . 4 1 1 ) N e a r b y

How can I achieve this in PHP?

MagisterMundus
  • 335
  • 4
  • 18

3 Answers3

1

I took @user1978142 code and modified it a bit so it would display fine on my MAC terminal.

Here is a working version:

<?php

$string = '0053 004F 0053 0028 004C 0029 003A 0053 0068 0069 006D 0069 006E 0020 0046 0061 0069 
0072 0079 006C 0061 006E 0064 0020 0057 0065 0073 0074 0020 0052 0064 002C 0048 0075 
0069 0063 0068 0065 006E 0067 002C 0048 0075 0069 007A 0068 006F 0075 002C 0047 0075 
0061 006E 0067 0064 006F 006E 0067 0028 004E 0032 0033 002E 0031 0031 0031 002C 0045 
0031 0031 0034 002E 0034 0031 0031 0029 004E 0065 0061 0072 0062 0079';

$final_string = '';
foreach(explode(' ', $string) as $string) {
    $final_string .= chr('0x'.$string);
}

echo "\n".$final_string."\n";
?>
user1978142
  • 7,946
  • 3
  • 17
  • 20
MagisterMundus
  • 335
  • 4
  • 18
0

Possible Duplicate:

How to get the character from unicode code point in PHP?

PHP: Convert unicode codepoint to UTF-8

If I understand correctly, you can view these topics on stackoverflow.

Community
  • 1
  • 1
user3566301
  • 182
  • 1
  • 13
0

You need to explode each string first, then append &#x on each strings so that you can utilize html_entity_decode(). Consider this example:

$string = '0053 004F 0053 0028 004C 0029 003A 0053 0068 0069 006D 0069 006E 0020 0046 0061 0069 
0072 0079 006C 0061 006E 0064 0020 0057 0065 0073 0074 0020 0052 0064 002C 0048 0075 
0069 0063 0068 0065 006E 0067 002C 0048 0075 0069 007A 0068 006F 0075 002C 0047 0075 
0061 006E 0067 0064 006F 006E 0067 0028 004E 0032 0033 002E 0031 0031 0031 002C 0045 
0031 0031 0034 002E 0034 0031 0031 0029 004E 0065 0061 0072 0062 0079';

$final_string = '';
foreach(explode(' ', $string) as $string) {
    $final_string .= html_entity_decode('&#x' . trim($string));
}

echo $final_string;
// SOS(L):Shimin Fairyland West Rd,Huicheng,Huizhou,Guangdong(N23.111,E114.411)Nearby
user1978142
  • 7,946
  • 3
  • 17
  • 20
  • Thanks, that worked. Anyway to convert the html entities to something that can be read at a UNIX terminal? I tried converting to utf-8 with no success so far... – MagisterMundus Jun 30 '14 at 13:56
  • @MagisterMundus in this case its in HTML format, but in terms of CLI, its another story, since this unicode equivalent is for html only, yes. naturaly this wont work, i think for cli its something like `\u0053` something, you could google it yourself too since i dont memorize them all – user1978142 Jun 30 '14 at 14:06