The following code get result from google by a function. This all works great I only want sort the results.
How can I sort the records in the foreach loop alphabetical ASC...?
function fetch_google($terms="sample search",$numpages=1,$user_agent='Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0')
{
$searched="";
for($i=0;$i<=$numpages;$i++)
{
$ch = curl_init();
$url="http://www.google.com/search?hl=en&q=".urlencode($terms)."&start=".$i.'0';
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_USERAGENT, $user_agent);
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_REFERER, 'http://www.google.com/');
curl_setopt ($ch,CURLOPT_CONNECTTIMEOUT,120);
curl_setopt ($ch,CURLOPT_TIMEOUT,120);
curl_setopt ($ch,CURLOPT_MAXREDIRS,10);
curl_setopt ($ch,CURLOPT_COOKIEFILE,"cookie.txt");
curl_setopt ($ch,CURLOPT_COOKIEJAR,"cookie.txt");
$searched=$searched.curl_exec ($ch);
curl_close ($ch);
}
$xml = new DOMDocument();
@$xml->loadHTML($searched);
foreach($xml->getElementsByTagName('a') as $lnk)
{
if($lnk->getAttribute('class')=='l')
{
$links[] = array(
'href' => $lnk->getAttribute('href'),
'title' => $lnk->nodeValue
);
}
}
return $links;
}
$content = fetch_google("exemple",1);
foreach($content as $elem)
{
echo "<a target=\"_blank\" href=$elem[href]>$elem[title]</a><br>";
}
I want to sort the rows by $elem[title] ASC
Help is much appriciated!