doupo2157 2016-03-28 17:46
浏览 42
已采纳

打印功能的HTML页面编码[重复]

This question already has an answer here:

I haven't done PHP for a while so I apologize in advance if it's something simple.

I'm migrating a PHP-based site to a shared web hosting and there's an issue with the way it outputs HTML markup. Let's consider the following code snippet that reads HTML from a file on the server and outputs it to the web browser:

$handle = @fopen($file_name, "rb");

$str = @fread($handle, filesize($file_name));
fclose($handle);

print $str;

For some reason it replaces some characters on the page. Like it puts that character instead of an apostrophe:

enter image description here

while it looked normal when the exact same code ran from a previous web hosting server:

enter image description here

Any idea what am I missing here?

</div>
  • 写回答

1条回答 默认 最新

  • dongnan4571 2016-03-28 17:55
    关注

    Thats an encode error, if your text have some especial characters, some encode types doesn't recognize it correctly..

    Try put your header to recognize utf-8 encode, add this (inside you php tag, at the beginning!) to your code

    header('Content-Type: text/html; charset=utf-8');
    file_put_contents($myFile, "\xEF\xBB\xBF".  $content); 
    

    And add this meta tag inside your html head

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

    UPD:

    I've done some research on php reference(http://php.net/manual/en/function.fopen.php#104325), and i found something! Try use this php function to solve your problem:

    <?php 
    function utf8_fopen_read($fileName) { 
        $fc = iconv('windows-1250', 'utf-8', file_get_contents($fileName));
        $handle=fopen("php://memory", "rw"); 
        fwrite($handle, $fc); 
        fseek($handle, 0); 
        return $handle; 
    } 
    ?> 
    

    Example of how to use:

    Example usage: 
    
    <?php 
    $fh = utf8_fopen_read($filename); 
    while (($data = fgetcsv($fh, 1000, ",")) !== false) { 
        foreach ($data as $value) { 
            echo $value . "<br />
    "; 
        } 
    } 
    ?> 
    

    Hope it helps!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路