Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php URL请求返回奇怪字符而不是重音的结果_Php_Simple Html Dom_Google Search - Fatal编程技术网

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

Php URL请求返回奇怪字符而不是重音的结果,php,simple-html-dom,google-search,Php,Simple Html Dom,Google Search,我的问题是,print\r()的输出中没有显示重音符号 这是我的密码: <?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); $r

我的问题是,
print\r()
的输出中没有显示重音符号

这是我的密码:

<?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);
}
?>

您认为解决方案是什么?

您可以做的三件基本事情:

  • 将页面编码设置为UTF-8-在页面的最开始添加:
    header('Content-Type:text/html;charset=UTF-8')
  • 确保代码文件保存为UTF-8(无BOM)
  • 添加一个函数,将解析后的字符串转换为UTF-8(以防其他站点使用不同的编码)
  • 您的代码应该是这样的(经过测试-使用英语和希伯来语结果进行了很好的尝试):


    这是一个编码问题。(UTF-8)准备被谷歌禁止:-DGoogle会在几次类似这样的查询后将你列入黑名单:P,你最好的选择是使用他们的。谢谢,但我想知道关于编码错误的答案我猜答案是gzipped。使用
    file\u get\u html
    您将无法从google获得可用的响应,谢谢您的帮助,但您有必要测试代码。它没有给出解决方案。@Anas El Fakir-我测试过它,您可能忘了在页面url中设置q变量-编辑的代码(添加了“忘记q…”)。它工作得很好,尝试了英语、希伯来语、阿拉伯语。请确保您的文档(在编辑器中)设置为编码UTF-8(无BOM)或ANSI为UTF-8。
    Array ( [0] => �t� m (plural �t�s)...
    
     <?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.";
     } 
     ?>