dt1888 2014-10-10 00:25
浏览 29

URL请求的结果返回奇怪的字符而不是重音

My problem is that the accents are not displayed in the output of print_r().

Here is my code:

<?php
include('./lib/simple_html_dom.php');
error_reporting(E_ALL);
if (isset($_GET['q'])){
$q = $_GET['q'];
$keyword=urlencode($q);
$url="https://www.google.com/search?q=$keyword";
$html=file_get_html($url);
$results=$html->find('li.g');
$G_tot = sizeof($results)-1;
for($g=0;$g<=$G_tot;$g++){
$results=$html->find('li.g',$g);
$array_ttl_google[]=$results->find('h3.r',0)->plaintext;
$array_desc_google[]=$results->find('span.st',0)->plaintext;
$array_href_google[]=$results->find('cite',0)->plaintext;
}
print_r($array_desc_google);
}
?>

Here is the result of print_r:

Array ( [0] => �t� m (plural �t�s)...

What is the resolution in your opinion?

  • 写回答

1条回答 默认 最新

  • doutan3192 2014-10-10 02:28
    关注

    3 basic things you can do:

    1. Set the page encoding to UTF-8 - Add at the very begining of your page: header('Content-Type: text/html; charset=utf-8');
    2. Make sure your code file is saved as UTF-8 (without BOM).
    3. Add a function to translate the parsed string to UTF-8 (in case some other sites are using different encodings)

    Your code should look something like that (Tested - working great tried with english and hebrew results):

     <?php
     header('Content-Type: text/html; charset=utf-8');
    
     include('simple_html_dom.php');
     error_reporting(0);
     if (isset($_GET['q'])){
         $q = $_GET['q'];
         $keyword=urlencode($q);
         $url="https://www.google.com/search?q=$keyword";
         $html=file_get_html($url);
    
         //Make sure we received UTF-8:
         $encoding = @mb_detect_encoding($html);
         if ($encoding && strtoupper($encoding) != "UTF-8")
            $html = @iconv($encoding, "utf-8//TRANSLIT//IGNORE", $html);
    
         //Proceed with your code:
         $results=$html->find('li.g');
         $G_tot = sizeof($results)-1;
         for($g=0;$g<=$G_tot;$g++){
             $results=$html->find('li.g',$g);
             $array_ttl_google[]= $results->find('h3.r',0)->plaintext;
             $array_desc_google[]= $results->find('span.st',0)->plaintext;
             $array_href_google[] = $results->find('cite',0)->plaintext;
          }
          print_r($array_desc_google);
     } else {
        echo "You forgot to set the 'q' variable in your url.";
     } 
     ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看