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);
}
?>
您认为解决方案是什么?您可以做的三件基本事情:
header('Content-Type:text/html;charset=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.";
}
?>