Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert utf8 to latin1 in PHP. All characters above 255 convert to char references

I need to convert text in UTF-8 into text encoded in ISO-8859-1 such that any character that are not part of ISO-8859-1 set would turn into character references. (ex β)

Example: I want to turn text like

hello é β 水

into

hello é β 水

I am doing all this in PHP. I tried built-in functions, iconv, and tidy and combination of those and still cant get a reliable solution.

Here is what I have so far

// convert any characters fount in the entity table into HTML entities
// do not double encode entities, do not mess with quotes
// use UTF-8 as character encoding because the page submits UTF-8
$str = htmlentities($str,ENT_NOQUOTES,'UTF-8',false);
//print $str."\n";

// convert text from UTF-8 to ISO-8859-1, 
// characters that cannot be converted will be converted to ?
$str = utf8_decode($str);
//print $str."\n";    

// make string XML valid.
// mainly it converts text entities into numeric entities.
$opts = array(  "output-xhtml"      => true, 
            "output-xml"        => true, 
            "show-body-only"    => true,
            "numeric-entities"  => true,
            "wrap"              => 0,
            "indent"            => false,
            "char-encoding" => 'latin1'
        );
$tidy = tidy_parse_string($str, $opts,'latin1');
tidy_clean_repair($tidy);
$str = tidy_get_output($tidy);      
//print $str."\n";
like image 351
Mike Starov Avatar asked Jul 12 '10 20:07

Mike Starov


1 Answers

You'll need multibyte support. In particular, mb_encode_numericentity():

$convmap= array(0x0100, 0xFFFF, 0, 0xFFFF);
$encutf= mb_encode_numericentity($utf, $convmap, 'UTF-8');
$iso= utf8_decode($encutf);

(This doesn't touch <, &, " etc so you may also need htmlspecialchars() beforehand.)

like image 173
bobince Avatar answered Nov 04 '22 13:11

bobince