dongxieting9623
dongxieting9623
2013-03-08 23:25

PHP file_get_contents特定编码

已采纳

i have one special issue with file_get_contents in PHP, here is code:

$html = file_get_contents("http://www.bratm.sk/trochu-harlem-inspiracie-zaslite-nam-svoj-super-harlem-shake-ak-bude-husty-zverejnime-ho");

it gives me something like this

í}KsŰĆŇčÚŽň!Ç& zRŚ|iI[#)öIkI ĆĺăĹŮÝĹÍý_ŐŃâ[EVßîV%Ů˙ëvĎ ř)śG#óčééééÇĚ çáÜöÁÖÉ;¤áˇ,rřĂăçť[DQ5íeaKÓśOśÉ?ě='šL¸ÔöLßä6ľ4mg_!JĂ÷˘Śu:L§án];9ŇÎV+ި1|C

in some pages from this page i can get proper encoding and content with iconv, but here im helpless, how can i fix that? thx

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • dongrang9300 dongrang9300 8年前

    Use cURL. This function is an alternative to file_get_contents.

    function url_get_contents($Url) {
    if (!function_exists('curl_init')){
        die('CURL is not installed!');
    }
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $Url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    $output = curl_exec($ch);
    curl_close($ch);
    return $output;
    }
    $data = url_get_contents("http://www.bratm.sk/trochu-harlem-inspiracie-zaslite-nam-svoj-super-harlem-shake-ak-bude-husty-zverejnime-ho/");
    print_r($data);
    
    点赞 评论 复制链接分享
  • duanci5913 duanci5913 8年前

    I think you're looking for something like this

    $opts = array('http' => array('header' => 'Accept-Charset: UTF-8, *;q=0'));
    $context = stream_context_create($opts);
    
    $filename = "http://www.bratm.sk/trochu-harlem-inspiracie-zaslite-nam-svoj-super-harlem-    shake-ak-bude-husty-zverejnime-ho";
    echo file_get_contents($filename, false, $context);
    
    点赞 评论 复制链接分享
  • dsvd407787736 dsvd407787736 8年前

    That page is in UTF-8. You need to set the header to match:

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

    点赞 评论 复制链接分享

相关推荐