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!

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

报告相同问题?

悬赏问题

  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了
  • ¥100 H5网页如何调用微信扫一扫功能?
  • ¥15 讲解电路图,付费求解
  • ¥15 有偿请教计算电磁学的问题涉及到空间中时域UTD和FDTD算法结合的
  • ¥15 three.js添加后处理以后模型锯齿化严重