Version: 1.0

Type: Full Script

Category: HTML

License: GNU General Public License

Description: This is a script that I created for use on my intranet at work. It's really basic, it doesn't come with a lot of bells and whistles, and I realize that something bigger, (and better), has probably already been posted, but for those that can use my humble effort, I offer it up. Basically, it's just a search script that allows the user (via the form), to select which search engine they'd like to use, and then to submit a search query. That aspect of it was *really* simple, and if that's all this was, I'd probably not bother posting it. However, in an effort to emulate the behavior of Google Toolbar, I wanted the user to be able to go directly to a specific URL if they typed one in. So, by using some really basic regular expressions, the script will "sense" when someone has typed in an actual URL, and will take them to that URL instead of performing a search. That's all there is to it, and I'd warn that I'm very new to regular expressions. If there's anyone who can suggest a better one to use for URL validation, I'd really appreciate it.



<?php

/*

Search Engine Switching and "Smart URL" script

I'm restructuring this script to incorporate the use of regular expressions, which I'm hoping will clean up and 
simplify the code considerably.  The basic function of this script remains the same...it allows the user to 
choose what search engine they want to go with, and then to search with that engine.  Alternatively, it 
features "smart" URL checking, in that if it senses a URL, it will take the user to the URL they indicated.






*/


//Post

$Engine=$_POST['engine'];
$Query=$_POST['searchbox'];


//RegEx--The variables holding our regular expressions


$URLReg='/^w{3}\.[a-z0-9-_.]+\.[a-z]{2,4}/'; //Tests for a valid URL beginning in 'www.'

$URLReg2='/^[a-z0-9-_.]+\.[a-z]{2,4}/'; //Tests for a valid URL that has dropped the 'www.'


$URLReg3='/^http:\/\/{1}w{3}?\.?[a-z0-9-_.]+\.[a-z]{2,4}/'; //Tests for a valid URL that has the 'http://' preface, with or without 'www.'


$URLReg4='/^http:\/\/[a-z0-9-_.]\/?/';//Tests for http://<location> type URL's, such as with "http://localhost/



//RegEx Tests--Variables to hold the result of comparing our Query to our Regex's


$StandardURL= preg_match($URLReg,$Query);

$ShortURL= preg_match($URLReg2,$Query);

$LongURL= preg_match($URLReg3,$Query);

$LocalURL= preg_match($URLReg4,$Query);



if($StandardURL!=false or $ShortURL!=false) //If the query is a standard or short URL
                     
					 {
					 
					 header ("Location: http://".$Query); //We send them to the URL.
					 
					 }


elseif($LongURL!=false or $LocalURL!=false)//If it is a long URL, or a local one.

                     {
					   header("Location: ".$Query);
					 
					 
					 }
				
				
else //If the query is not a URL of any discernable kind, do a search.
     {
	 
	 switch ($Engine)
				
				           {
				
								case'yahoo': 
								
										  $Query="http://search.yahoo.com/search?p=".$Query;
										  header("Location:".$Query );
										  break;
								
								
								case 'msn':
								
								   
										
										 $Query="http://search.msn.com/results.aspx?q=".$Query;
										 header("Location:".$Query );
										 break;
								
								
								
								
								
								case 'google':
								
										 $Query="http://www.google.com/search?q=".$Query;
										 header("Location:".$Query);
										 break;
										
				            }
		

         }


?>

##################################################################################################
/*Form that interacts with the script above: */

<form name="search" method="post" action="search.php" target="_blank">


<input type="text" size="40" name="searchbox" /><br />

<input type="radio" name="engine" value="google" checked="yes" />Google&nbsp;
<input type="radio" name="engine" value="yahoo" />Yahoo&nbsp;
<input type="radio" name="engine" value="msn" />MSN<p />





<input type="submit" name="submit" value="Search The Web" /><br />
</form>