Преобразование utf8 в latin1 в PHP. Все символы выше 255 преобразуются в ссылки на char

Мне нужно преобразовать текст в UTF-8 в текст, закодированный в ISO-8859-1, так что любой символ, который не является частью набора ISO-8859-1, превратится в ссылки на символы. (ex β )

Пример: я хочу повернуть текст, как

 hello é β 水 

в

 hello é β 水 

Я делаю все это в PHP. Я пробовал встроенные функции, iconv, и аккуратно, и комбинацию этих и все еще не могу найти надежное решение.

Вот что я до сих пор

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

Вам понадобится многобайтовая поддержка. В частности, mb_encode_numericentity () :

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

(Это не касается < , & " т. Д., Поэтому вам также может потребоваться htmlspecialchars() .)