我需要將UTF-8中的文本轉換爲ISO-8859-1編碼的文本,這樣任何不屬於ISO-8859-1集的字符都會變成字符引用。 (前β
)在PHP中將utf8轉換爲latin1。 255以上的所有字符轉換爲字符引用
例子:我想變成像
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";
使用'ヶ輛( '你好éβ水',ENT_COMPAT「 UTF-8'),你至少可以將'é'和'β'轉換爲HTML實體(命名實體)。 – NikiC 2010-07-12 20:15:05
當然這還不夠。最後一個字符是這裏的主要問題。請不要在最終結果(XML數據)中不允許實體,並且我希望將ISO-8859-1設置爲字符。 – 2010-07-12 20:51:38