Php 将UTF-8中的HTML实体转换为SHIFT_JIS

Php 将UTF-8中的HTML实体转换为SHIFT_JIS,php,character-encoding,html-entities,Php,Character Encoding,Html Entities,我正在与一个网站合作,该网站需要针对不支持Unicode的老式日本手机。问题是,站点的文本作为HTML实体保存在数据库中(即,Ӓ;)。这个数据库绝对不能更改,因为它用于数百个网站 我需要做的是将这些实体转换为实际字符,然后在发送之前转换字符串编码,因为手机在不首先转换实体的情况下呈现实体 我尝试了mb\u convert\u编码和iconv,但他们所做的只是转换实体的编码,而不是创建文本 提前谢谢 编辑: 我还尝试了html\u entity\u decode。它正在产生相同的结果——

我正在与一个网站合作,该网站需要针对不支持Unicode的老式日本手机。问题是,站点的文本作为HTML实体保存在数据库中(即,Ӓ;)。这个数据库绝对不能更改,因为它用于数百个网站

我需要做的是将这些实体转换为实际字符,然后在发送之前转换字符串编码,因为手机在不首先转换实体的情况下呈现实体

我尝试了
mb\u convert\u编码
iconv
,但他们所做的只是转换实体的编码,而不是创建文本

提前谢谢

编辑:

我还尝试了
html\u entity\u decode
。它正在产生相同的结果——一个未转换的字符串

这是我正在处理的示例数据

预期结果:シェラトン・ヌーサリゾート&スパ

HTML代码:
和#12471ェラトン・ヌーサリゾート&スパ

html_entity_decode([上面的字符串]、ENT_COMPAT、'SHIFT_JIS')的输出
与输入字符串相同。

我想您只需要

编辑:根据您的编辑:

$output = preg_replace_callback("/(&#[0-9]+;)/", create_function('$m', 'return mb_convert_encoding($m[1], "UTF-8", "HTML-ENTITIES"); '), $original_string); 
请注意,这只是将实体转换为实际字符的第一步。

我在上找到此函数,它适用于您的示例:

function unhtmlentities($string) {
    // replace numeric entities
    $string = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string);
    $string = preg_replace('~&#([0-9]+);~e', 'chr("\\1")', $string);
    // replace literal entities
    $trans_tbl = get_html_translation_table(HTML_ENTITIES);
    $trans_tbl = array_flip($trans_tbl);
    return strtr($string, $trans_tbl);
}
$originalEncoding = 'UTF-8'; // that's only assumed, you have not shared the info so far
$targetEncoding = 'SHIFT_JIS';
$string = '... whatever you have ... ';
// superfluous, but to get the picture:
$string = mb_convert_encoding($string, 'UTF-8', $originalEncoding);
$string = html_entity_decode($string, ENT_COMPAT, 'UTF-8');
$stringTarget = mb_convert_encoding($string, $targetEncoding, 'UTF-8');

只需注意,您正在从实体中创建正确的代码点。例如,如果原始编码为UTF-8:

function unhtmlentities($string) {
    // replace numeric entities
    $string = preg_replace('~&#x([0-9a-f]+);~ei', 'chr(hexdec("\\1"))', $string);
    $string = preg_replace('~&#([0-9]+);~e', 'chr("\\1")', $string);
    // replace literal entities
    $trans_tbl = get_html_translation_table(HTML_ENTITIES);
    $trans_tbl = array_flip($trans_tbl);
    return strtr($string, $trans_tbl);
}
$originalEncoding = 'UTF-8'; // that's only assumed, you have not shared the info so far
$targetEncoding = 'SHIFT_JIS';
$string = '... whatever you have ... ';
// superfluous, but to get the picture:
$string = mb_convert_encoding($string, 'UTF-8', $originalEncoding);
$string = html_entity_decode($string, ENT_COMPAT, 'UTF-8');
$stringTarget = mb_convert_encoding($string, $targetEncoding, 'UTF-8');

为了参与其中,我在编码时遇到了某种编码错误,我建议使用以下代码段:

 $string_to_encode=" your string ";
 if(mb_detect_encoding($string_to_encode)!==FALSE){
      $converted_string=mb_convert_encoding($string_to_encode,'UTF-8');
 }

对于大量数据来说可能不是最好的,但仍然有效。

html\u entity\u decode()
对它们无效吗?我已经更新了这个问题<代码>html实体解码转换不正确。@MattBelanger:原始字符串的编码是什么?@hakre原始字符串是UTF-8格式的,这就是html实体解码最初无法工作的原因。解决了这个问题,然后使用iconv进行转换,似乎已经解决了这个问题。@MattBelanger:好的,这正是我的假设,请参见下面的答案:)