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?

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.";
 } 
 ?>