...

View Full Version : PHP and Currency Symbols (displaying special chars nightmare)



hinch
02-11-2010, 01:39 PM
HELP :)

I'm having a nightmare handling currency special chars.

I'm using the class below to convert inputted currency symblls into the NCR code for that symbol.


class unicode_replace_entities {
public function UTF8entities($content="") {
$contents = $this->unicode_string_to_array($content);
$swap = "";
$iCount = count($contents);
for ($o=0;$o<$iCount;$o++) {
$contents[$o] = $this->unicode_entity_replace($contents[$o]);
$swap .= $contents[$o];
}
return mb_convert_encoding($swap,"UTF-8"); //not really necessary, but why not.
}

public function unicode_string_to_array( $string ) { //adjwilli
$strlen = mb_strlen($string);
while ($strlen) {
$array[] = mb_substr( $string, 0, 1, "UTF-8" );
$string = mb_substr( $string, 1, $strlen, "UTF-8" );
$strlen = mb_strlen( $string );
}
return $array;
}

public function unicode_entity_replace($c) { //m. perez
$h = ord($c{0});
if ($h <= 0x7F) {
return $c;
} else if ($h < 0xC2) {
return $c;
}

if ($h <= 0xDF) {
$h = ($h & 0x1F) << 6 | (ord($c{1}) & 0x3F);
$h = "&#" . $h . ";";
return $h;
} else if ($h <= 0xEF) {
$h = ($h & 0x0F) << 12 | (ord($c{1}) & 0x3F) << 6 | (ord($c{2}) & 0x3F);
$h = "&#" . $h . ";";
return $h;
} else if ($h <= 0xF4) {
$h = ($h & 0x0F) << 18 | (ord($c{1}) & 0x3F) << 12 | (ord($c{2}) & 0x3F) << 6 | (ord($c{3}) & 0x3F);
$h = "&#" . $h . ";";
return $h;
}
}
}

Now this code works fine if I just immediately spit the encoded string back out to the page using echo. however I want to store it all in a database I simply insert the data into the DB table and I end up with this


ID ShortCode Symbol
1 GBP & #163
2 USD $
3 Euro & #836
4 BGN & #108
5 CNY & #165
6 CRC & #835
7 CUP & #836
8 INR & #836


Which is exactly right.
However now when I pull from the DB to echo out back to screen isntead of echoing out the special chars correctly as it did before it now no longer spits out anything apart from the Yen, Pound and Dollar sign but it is echoing out the right NCR code just isn't being displayed (however it was being displayed correctly before insert to db)

See attachment for screenshot of what it looks like after db retreval.


<meta http-equiv="content-type" content="text/html;charset=utf-8" />
is set on the page.

Dormilich
02-11-2010, 04:33 PM
maybe the used font doesn’t contain the currency glyphs? check out the source code whether the entities were printed.

hinch
02-11-2010, 06:32 PM
yeah it does contain them because it displays them fine echoing them out before db insertion its only on db retreval that they don't display correctly

Dormilich
02-12-2010, 07:49 AM
erm, are the entities in the HTML source code or not?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum