Created
March 25, 2017 15:06
-
-
Save mkdesignn/97d5c3e2c3359a5257dff8e5e8244cfc to your computer and use it in GitHub Desktop.
Retrieve Search result from google
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getSearchResult($keyword){ | |
$subject = file_get_contents("http://www.google.com/search?q=".urlencode($keyword)); | |
$href_pattern = '/<h3 class="r">(.*?)<\/h3>/s'; | |
preg_match_all($href_pattern, $subject, $matches); | |
foreach ($matches[0] as $key => $match) { | |
$replacment_pattern = '/href="([^"]+)"/'; | |
preg_match($replacment_pattern, $match, $found); | |
$found = explode("&", $found[1]); | |
$found[0] = str_replace("/url?q=", "", $found[0]); | |
$found[0] = urldecode($found[0]); | |
$replace_url = preg_replace($replacment_pattern, "href='".$found[0]."'", $match); | |
settype($replace_url, "string"); | |
if( gettype($replace_url) != "boolean" ) | |
$matches[0][$key] = $replace_url; | |
else | |
unset($matches[0][$key]); | |
if( json_encode($matches[0][$key]) == false ) | |
unset($matches[0][$key]); | |
} | |
return $matches[0]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment