Location: PHPKode > scripts > Simple Google Custom Business Search Script > simple-google-custom-business-search-script/googleSearch.php
<?php
/*

AUTHOR: Jamie Estep
WEBSITE: http://www.ecommerce-blog.org

LICENSE:

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

For a copy of the GNU General Public License see 
<http://www.gnu.org/licenses/>

*/
?>

<!-- Start Search Box -->
<table width="100%" border="0" cellspacing="0" cellpadding="10">
  <tr>
    <td align="center" valign="middle">
	
	<form action="site-search.php">
		<input name="query" type="text" size="40" value="<?php if(isset($_GET['query'])){echo urldecode(cleanSearch($_GET['query']));} ?>" />
		<input type="submit" name="sa" value="Search" />
		<img src="http://google.com/coop/images/google_custom_search_smnar.gif" alt="Google Custom Search" />
   </form>
	
	</td>
  </tr>
</table>
<!-- End Search Box -->

<?php

//constants
$accountId = ''; //put google search ID Here - Should be a long string of numbers letters and symbols
$collation  = 'ISO-8859-1'; //change this if you want something other than ISO-8859-1, IE: ut8-f
$numberPerPage = '10'; //this is how many results you want per page
$noResults = 'There were no results found for your search. Please try again.'; //Message if no results are found

//check if the form was submitted
if(!empty($_GET['query'])){
	
	//clean the input so that it is safe
	$searchTerm = cleanSearch($_GET['query']);
	
	//cast $_GET['start'] to an integer just in case
	$start = (int)$_GET['start'];
	
	//build the google query here
	$buildQuery = 'http://www.google.com/search?cx='.$accountId.'&client=google-csbe&start='.$start.'&num='.$numberPerPage.'&output=xml_no_dtd&q='.$searchTerm.'&ie='.$collation.'&oe='.$collation;
	
	//get the result for the query from google
	$thisSearch = file_get_contents("$buildQuery");
	
	//instantiate a simple xml class and process the google result
	$xml = new SimpleXMLElement($thisSearch);
	
	//find the number of results for pagination later on
	$total = count($xml->RES->R);
	
	//Display Nothing found message if there are no results.
	if($total < 1){
	
		$output .=  "<p>$noResults</p>";
	
	}
	
	//loop through the results 
	foreach ($xml->RES->R as $key){
		
		//the replaceChars removes anything you need it to, I found google returning some unusable characters, and you can fix it with this function
		//Edit the actual find / replace at the bottom
		
		//format the output for each result
		$output .=  '
		<p><a href="'.$key->U.'">'.replaceChars($key->T).'</a><br />
		'.replaceChars($key->S).'<br />
		<span style="color:#060;">'.$key->U.'</span></p>
		';
	
	}
	
	//simple pagination
	$output .= '
	<table width="100%" border="0" cellspacing="0" cellpadding="5">
		<tr>
	';
	 	
		//make sure we dont display previous page on the first page
		if ($start > 0){
		
			$output .= '<td align="left"><a href="?query='.$searchTerm.'&start='.($start - $numberPerPage).'">&laquo; Previous 10</a></td>';
		
		}
		
		//make sure we dont display next page on the last page
		if ($total == $numberPerPage){
			
			$output .= '<td align="right"><a href="?query='.$searchTerm.'&start='.($start + $numberPerPage).'">Next 10 &raquo;</a></td>';
				
		}
	
	$output .= '
		</tr>
	</table>
	';
	
	echo $output;

}

function cleanSearch($input){
	//this will clean the query so that it is safe to use for whatever
	//this is broken into steps, but can be done on a single line if prefered
	
	//trim whitespace
	$input = trim($input);
	
	//if you would like your users to be able to search for code, you can comment this out
	//Make sure you know what you're doing before you comment this out!
	$input = strip_tags($input);
	
	//Convert html characters to a safe format, IE: & - &amp; etc.
	$input = htmlspecialchars($input);
	
	//convert to a url safe code since we use the GET function to request from google
	$input = urlencode($input);
	
	return $input;
}

function replaceChars($input){
//using this function to replace some html from the data returned by google
//Make sure there is a corresponding value in the find and replace arrays or you will have problems
	
	//these are what you want to replace
	$find = array(
	'»',
	'<br>'
	);
	
	//these are what you need to replace them with
	$replace = array(
	'&raquo;',
	''
	);
	
	return str_replace($find,$replace,$input);

}

?>
Return current item: Simple Google Custom Business Search Script