PHP UTF-8到GB2312 [英] PHP UTF-8 to GB2312

查看:82
本文介绍了PHP UTF-8到GB2312的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们的Web应用程序的一部分有一点Ajax方法,将加载iFrame中的页面或允许您下载。



我们存储一堆搜索结果从搜索引擎,我们有脚本打开包含我们的信息和搜索html的文件。我们从顶部(我们的信息)中删除我们不需要的东西,然后我们通过回调$ html变量或将其放在临时文件中,并将其删除下载来提供。



问题:我加载了iFrame中的页面,并加载了UTF-8,因为其他的都是。如果我手动下载文件是很好的,FF告诉我,编码是x-gbk。



我尝试使用mb_convert_encoding无效。我们在这个服务器上使用PHP4。



想法?



编辑:驱动此代码



$ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ 位置:的index.php);
}
$ download = false;

如果(!isset($ _ GET ['view'])|| $ _GET ['view']!='true')
{
$ download = true;
}

$ file = LOG_PATH。 $ _GET [文件];

$ fileName = end(explode(/,$ file));

$ fh = fopen($ file,rb);

如果(!$ fh)
{
echo处理此文件时出错,请重试。
return;
}

//打开HTML文件,在顶部打开垃圾,在所有images /之前注入http://google.com
$ html = fread( $ fh,filesize($ file));
fclose($ fh);

//需要修剪我们的标题
$ htmlArr = explode(<!,$ html,2);
$ htmlArr [1] =<! 。 $ htmlArr [1];

if(strstr($ file,google))
{
$ html = str_replace('src =/ images /','src =http: /google.com/images/',$ htmlArr [1]);
$ html = str_replace('href =/','href =http://google.com/',$ html);
}
else if(strstr($ file,/ msn /))
{
$ html = str_replace('src =/ images /','src = http://bing.com/images/,$ htmlArr [1]);
$ html = str_replace('href =/','href =http://www.bing.com/',$ html);
}
else
{
$ html = $ htmlArr [1];
}

if(strstr($ file,baidu))
{
$ html = mb_convert_encoding($ html,'utf-8'); //不起作用
}

if($ download)
{
//写入临时文件
$ fh = fopen(/ tmp /。$ fileName,'w +');
fwrite($ fh,$ html);
fclose($ fh);

$ fh = fopen(/ tmp /。$ fileName,rb);
header('Content-type:application / force-download;');
header(Content-Type:text / html;);
header('Content-Disposition:attachment; filename ='。$ fileName。'');
fpassthru($ fh);
fclose($ fh);
unlink(/ tmp /。$ fileName);
}
else // AJAX调用
{
echo $ html;
}


解决方案

您可能想尝试iconv ()而不是mb_convert_encoding() - 它支持更广泛的一组编码。


Part of our web app has a little Ajax method that will load a page in an iFrame or allow you to download it.

We store a bunch of search results from search engines and we have script opens the file containing our info and the search html. We strip out the stuff we don't need from the top (our info) and then we serve that up either by echo'ing the $html variable or putting it in a temporary file and dishing it off to download.

The problem: I load the page in the iFrame and it's loaded in UTF-8 because everything else is. If I download the file manually it is fine and FF tells me the endoding is x-gbk.

I've tried using mb_convert_encoding to no avail. We are using PHP4 on this server.

Thoughts?

EDIT: Code that drives this

f(!isset($_GET['file']) || $_GET['file'] == '')
{
    header("location:index.php");
}
$download = false;

if(!isset($_GET['view']) || $_GET['view'] != 'true')
{
    $download = true;
}

$file = LOG_PATH . $_GET['file'];

$fileName = end(explode("/", $file));

$fh = fopen($file, "rb");

if(!$fh)
{
    echo "There was an error in processing this file. Please retry.";
    return;
}

// Open HTML file, rip out garbage at top, inject "http://google.com" before all "images/"
$html = fread($fh, filesize($file));
fclose($fh);

// Need to trim off our headers
$htmlArr = explode("<!", $html, 2);
$htmlArr[1] = "<!" . $htmlArr[1];   

if(strstr($file, "google"))
{
    $html = str_replace('src="/images/', 'src="http://google.com/images/', $htmlArr[1]);
    $html = str_replace('href="/', 'href="http://google.com/', $html);
}
else if(strstr($file, "/msn/"))
{
    $html = str_replace('src="/images/', 'src="http://bing.com/images/', $htmlArr[1]);
    $html = str_replace('href="/', 'href="http://www.bing.com/', $html);    
}
else
{
    $html = $htmlArr[1];
}

if(strstr($file, "baidu"))
{
    $html = mb_convert_encoding($html, 'utf-8'); // Does not work
}

if($download)
{   
    // Write to temporary file
    $fh = fopen("/tmp/" . $fileName, 'w+');
    fwrite($fh, $html);
    fclose($fh);

    $fh = fopen("/tmp/" . $fileName, "rb");
    header('Content-type: application/force-download;');
    header("Content-Type: text/html;");
    header('Content-Disposition: attachment; filename="' . $fileName . '"');
    fpassthru($fh);
    fclose($fh);
    unlink("/tmp/" . $fileName);
}
else // AJAX Call
{
    echo $html;
}

解决方案

You may want to try iconv() instead of mb_convert_encoding()--it has support for a much broader set of encodings.

这篇关于PHP UTF-8到GB2312的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆