Codesnipp.it Social Code Sharing

Andrew Champ

Keyword Search Websites

by Andrew Champ on Sep 09, 2010

&lt;?php // Compiled from 2 functions from http://phpsnippets.info. // I edited, added, & styled and put them together a Keyword Search on any website. // Not the prettiest way of doing it, but it works! // Example on http://andrewchamp.com/ if (isset($_GET['submit'])) { $url = $_GET['url']; $search = $_GET['search']; @$html_source = file_get_contents($url); $txt = html2txt($html_source); } function html2txt($document){ $search = array('@<script[^>]*?&gt;.*?</script>@si', // Strip JS '@<style[^>]*?&gt;.*?</style>@siU', // Strip Style (CSS) '@<[?]php[^>].*?[?]>@si', // Strip PHP '@<[?][^>].*?[?]>@si', // Strip PHP '@<[\\/\\!]*?[^<>]*?&gt;@si', // Strip HTML tags '@<![\\s\\S]*?--[ \\t\\n\\r]*>@' // Strip multi-line comments including CDATA ); $text = preg_replace($search, '', $document); return $text; } function highlight($sString, $aWords) { if (!is_array ($aWords) || empty ($aWords) || !is_string ($sString)) { return false; } $sWords = implode ('|', $aWords); return preg_replace ('@\\b('.$sWords.')\\b@si', '<span>$1</span>', $sString); } ?&gt; <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Keyword Search Websites</title> <style type="text/css"> *{padding:0; margin:0; outline:0; border:0;} body{font-size:14px; font-family:georgia, helvetica, arial, sans-serif; color:#666; line-height:1.5;} html{min-height:100%; height:auto; overflow-y:scroll;} #container{width:750px; margin:0 auto;} form{margin:10px 0 30px 0;} label{float:left; width:200px; padding:5px 0 0 5px; } input{border:1px solid #888; padding:5px; margin:5px;} #submit{background:#8DCF0C; color:#FFF;} #submit:hover{background:#7FAF20;} span{background:yellow;} #results{border:1px dashed #999; padding:10px;} #results .help{text-align:center;} #results h2{font-size:16px; font-weight:bold;} </style> </head> <body> <div id="container"> <form method="get" action="&lt;?php echo htmlentities($_SERVER['PHP_SELF']); ?&gt;"> <fieldset> <label for="url">URL: </label> <input name="url" id="url" value="http://" type="text" /> <br /> <label for="search">Search Term: </label> <input name="search" id="search" value="" type="text" /> <input type="submit" class="submit" id="submit" name="submit" value="Find!" /> </fieldset> </form> <hr /> <div id="results"> &lt;?php if (isset($_GET['submit'])) { echo "<h2>".$url." - ".$search."</h2>"."\\n<br />"; echo highlight( $txt, array($search) ); } else { echo "<div class='help'>Enter a URL and keyword to search. All results will be returned here.</div>"; } ?&gt; </div> </div> </body> </html>

Can't see the comments? Please login first :)