PHP编码与pdf文件的问题

时间:2016-10-31 13:52:06

标签: php encode

Ubuntu php 一起使用我遇到了一个常见问题,我没有找到任何解决方案。 我正在上传一个pdf文件,我将其转换为文本文件(使用ImgMagick + Tesseract)。

    $output = shell_exec('convert -density 300 ' . $fichier . ' ' . $fichier_noExt . '.png');
    $output = shell_exec('tesseract ' . $fichier_noExt . '.png ' . $fichier_noExt . '.txt');

当我这样做时:

$file = fopen($fichier_txt.'.txt', 'r+');
echo $file;

我得到一些'°'而不是'°','â,'而不是'€'和'é'而不是'é'。 我知道这是编码问题,但我找不到它。

2 个答案:

答案 0 :(得分:0)

如果要打印UTF-8字符串结果,可以尝试:

$file = fopen($fichier_txt.'.txt', 'r+');
while(!feof($file)){
 echo mb_convert_encoding(fread($file, 1024), 'UTF-8', mb_detect_encoding($file));
}
fclose($file);

文档:

http://php.net/manual/fr/function.mb-convert-encoding.php

http://php.net/manual/fr/function.mb-detect-encoding.php

您也可以使用dos2unix和mac2unix转换文件,使用此自定义函数:

function convertFiles($file) { // pass complete path to file
    if (shell_exec("dos2unix $file") !== FALSE) {
        if (shell_exec("mac2unix $file") !== FALSE) {
            return TRUE;
        }
        else {
            return FALSE;
        }
    }
    else {
        return FALSE;
    }
}

您可以使用apt-get install安装thoses命令 http://xmodulo.com/how-to-install-dos2unix-on-linux.html

最后,如果您在网页上显示它,请不要忘记设置元字符集内容类型:

header('Content-Type: text/html; charset=utf-8');

或html版

<meta http-equiv="Content-type" content="text/html; charset=utf-8" />

答案 1 :(得分:0)

哦亲爱的......

我只是忘了在我的文件上添加它:

header('Content-Type: text/html; charset=utf-8');

现在确实有用,对不起你的时间,但我需要一些新的看法:)。

祝你有个美好的一天!