首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PHP UTF-8到GB2312

PHP UTF-8到GB2312
EN

Stack Overflow用户
提问于 2010-07-16 22:22:34
回答 1查看 1K关注 0票数 2

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

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

问题是:我在iFrame中加载页面,它是以UTF8格式加载的,因为其他格式都是这样的。如果我手动下载文件,它是好的,并且FF告诉我内嵌是x-gbk。

我试过使用mb_convert_encoding,但都没有用。我们在此服务器上使用PHP4。

有什么想法?

编辑:驱动这一点的代码

代码语言:javascript
复制
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;
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-04-15 05:09:02

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/3265824

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档