我有一个存储在服务器中的文本文件,该文件以UTF-8编码,我试图从另一个服务器的网页加载该文件,该服务器在shift_JIS中编码。我不能让它工作。这是我的原始代码:
function whatever() {
$open_file = fopen("http://www.myass.com/whatever.txt", 'r');
$whatevers = explode("*", fgets($open_file));
$whatevers_num =count($whatevers)-2;
$i=1;
while($i <= $whatevers_num) {
$details = explode("|", $whatevers[$i++]);
echo <<<EOF
$details[1] - $details[2] - $details[3]
EOF;
}
fclose($open_file);
}
我试过这样做:
function whatever() {
$open_file = fopen(mb_convert_encoding("http://www.myass.com/whatever.txt", "shift_jis"), 'r');
$whatevers = explode("*", fgets($open_file));
$whatevers_num =count($whatevers)-2;
$i=1;
while($i <= $whatevers_num) {
$details = explode("|", $whatevers[$i++]);
echo <<<EOF
$details[1] - $details[2] - $details[3]
EOF;
}
fclose($open_file);
}
甚至这个:
function whatever() {
$open_file = fopen("http://www.myass.com/whatever.txt", 'r');
$whatevers = explode("*", fgets($open_file));
$whatevers_num =count($whatevers)-2;
$i=1;
while($i <= $whatevers_num) {
$details = explode("|", $whatevers[$i++]);
echo <<<EOF
$details[1] - $details[2] - $details[3]
EOF;
}
fclose($open_file);
}
ob_start();
whatever();
$get_whatever = ob_get_contents();
ob_end_clean();
$encode = mb_convert_encoding("$get_whatever", "shift_jis");
echo $encode;
但没有任何效果,我只得到乱码。我该怎么办?
谢谢。
答案 0 :(得分:0)
我建议将文件的整个内容(使用file_get_contents
或CURL)加载到字符串中并在进行任何其他类型的处理之前进行转换。这可以确保您拥有完整的转换文本。
$utf_string = file_get_contents('http://www.myass.com/whatever.txt');
$sjis_string = mb_convert_encoding($utf_string, 'SJIS', 'UTF-8');
echo($sjis_string);
在此之后,您可以启动explode
并循环逻辑。