Converting strings to match multiple charsets
| From: | Red Wingate | Date: | Wed, 30 Jun 2004 18:09:40 +0000 |
| Subject: | Converting strings to match multiple charsets | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-189329@lists.php.net to get a copy of this message | ||
Hey guys,
i ran into serious trouble when facing the problem to convert
data, which was retrieved from a single form in our CMS to match
the requirements of multiple charsets.
Our CMS uses UTF-8 which worked out quite fine to display the data
but caused weired symbols when displayed within a TEXTAREA or INPUT
field. So i tried to convert all characters beyound A-Z,0-9 to
HTML entities which worked out even better ( i used the following
functions to convert the data :
function func_ConvertToHTML ( $string ) {
$strlen = strlen ( $string ) ;
$return = '' ;
for ( $str_pos = 0 ; $str_pos < $strlen ; $str_pos++ ) {
$char = substr ( $string , $str_pos , 1 );
$ascii = ord ( $char );
if ( $ascii >> 5 == 6 ) {
$char2 = substr ( $string , ++$str_pos , 1 );
$ascii2 = ord ($char2);
$ascii &= 31 ;
$ascii2 &= 63 ;
$ascii2 |= ( ($ascii & 3 ) << 6 ) ;
$ascii >>= 2 ;
$return .= '&#x' . str_pad ( dechex( $ascii ) , 2 , '0' , STR_PAD_LEFT ) . str_pad ( dechex( $ascii2 ) , 2 , '0' , STR_PAD_LEFT ) . ';' ;
} else {
$return .= $char;
}
}
return $return;
}
But at this point i faced even bigger problems when using this kind
of data on JavaScripts or sending the data in an text/plain E-Mail.
I tryed to convert the data back but failed as chr() only supports
a Charset of 255 Characters ( which most languages don't match eg
ru, pl, ch, jp ... )
So my question is if anyone on this list has an idea on how to retrieve
the data completely? Some kind of func_ConvertFromHTML() function.
-- red