我需要将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