O'Reilly logo

PHP Hacks by Jack D. Herrington

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Hack #91. Search Google by Link Graph

Use Google's Web Services API and a Flikr-style link graph to search Google.

Google is a great search engine, but sometimes I find myself looking at the page snippets more than I do the pages themselves. This hack takes the snippets and looks for repeating words around the search term. It's a fascinating way to get more insight into a search phrase.

The Code

Save the code in Example 9-10 as index.php.

Example 9-10. A DHTML link graph that uses Google as a data source

<?php 
require_once("Services/Google.php");

$ignore = array( 
'the','for','and','with','the','new','are','but','its','that','was', 
'your', 'yours', 'also', 'all', 'use', 'could', 'would', 'should', 'when',
    'they', 
'far', 'one', 'two', 'three', 'you', 'most', 'how', 'these', 'there', 'now',
    'our', 
'from', 'only', 'here', 'will' ); 
$ignorehash = array(); 
foreach( $ignore as $word ) { $ignorehash[ $word ] = 1; }

$term = "Code Generation"; 
if( array_key_exists( 'term', $_GET ) ) 
  $term = $_GET['term'];

$key = "GOOGLE_KEY"; $google = new Services_Google( $key ); $google->queryOptions['limit'] = 50; $google->search( $term ); $data = array(); foreach($google as $key => $result) { $data []= array( 'title' => $result->title, 'snippet' => $result->snippet, 'URL' => $result->URL ); } function jsencode( $text ) { $text = preg_replace( '/\'/', '', $text ); return $text; } function get_words( $text ) { $text = preg_replace( '/\<(.*?)\>/', '', $text ); $text = preg_replace( '/[.]/', '', $text ); $text ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required