MagisterMundus
MagisterMundus

Reputation: 345

PHP - Convert unicode string to string?

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?

Upvotes: 0

Views: 1168

Answers (3)

MagisterMundus
MagisterMundus

Reputation: 345

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";
?>

Upvotes: 1

user1978142
user1978142

Reputation: 7948

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

Upvotes: 0

user3566301
user3566301

Reputation: 172

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.

Upvotes: 0

Related Questions