PHP Classes

File: simplesearch.php

Recommend this page to a friend!
  Classes of Jesus M. Castagnetto   Dictionary client class package   simplesearch.php   Download  
File: simplesearch.php
Role: ???
Content type: text/plain
Description: Example on a form to perform simple queries to a DICT server
Class: Dictionary client class package
Client dictionary class
Author: By
Last change:
Date: 24 years ago
Size: 5,285 bytes
 

Contents

Class file image Download
<!-- Simple example that uses pkg.dict.org (c) 2000, Jesus M. Castagnetto <jmcastagnetto@zkey.com> License: GPL, see www.gnu.org/copyleft/gpl.txt Changes: 2000/06/04 - Added the correct link to the GPL license 2000/06/05 - Fixes to bugs reported by Davor Cengija <davor@croart.com> (1) Links that look like {{UNIX}} were not being stripped correctly (2) Crossreference terms sometimes spanned a line break which looked real ugly/bad Thanks to Davor for reporting the bugs in this example 2000/06/05 - Found another bug, some crossreferences are to URLs and not terms in the dictionaries, the makeLink() function has been fixed to account for that. 2000/06/06 - Check for duplicates when creating the links, and modified the external URLs regex to recognize more protocols, as well as eliminate crossrefences to RFC's 2000/06/07 - Stoyan Jekov <jekov@cig.nml.mot.com> has sharp eye, and noticed that I forgot to close my HTML FORM element --> <?php $start=time(); include ("./pkg.dict.php"); ?> <html> <head> <title>Simple Form to Query dict.org</title> </head> <body> Search the dict.org server <p> <form action="<?php echo $PHP_SELF ?>" method="post"> <input type="text" name="query_term" size="60" <?php if ($query_term) echo "value=\"".$query_term."\""; ?> ><br> <input type="hidden" name="database" value="*"> <input type="hidden" name="strategy" value="exact"> <input type="submit" name="submit" value=" Search "> <input type="reset" name="reset" value=" Clear form input "> </form> <hr> <?php // check if element is in the array function inArray($element, $arr) { // figure out version list($major, $minor, $release) = explode(".", phpversion()); if (($major == 3 && $relesase >= 12) || $major == 4) { return in_array($element, $arr); } else { // assumes that we want to compare element value while (list($key, $val) = each($arr)) { if ($val == $element) return true; } return false; } } // remove duplicates from array // and eliminate the patterns in $nolinks function cleanArray($arr) { $nolinks = "rfc:"; $out = array(); for ($i=0; $i<count($arr); $i++) if (!inArray($arr[$i], $out) && !ereg($nolinks, $arr[$i])) $out[] = $arr[$i]; return $out; } //make the links to other words in the description function mkLinks($str, $db) { global $PHP_SELF; // modified the regexes to fix the bug reported by <davor@croart.com> $patt = "\{+([^{}]+)\}+"; $regex = "<b>\\1</b>"; $out = ereg_replace($patt, $regex, $str); $patt = "/\{+([^{}]+)\}+/"; preg_match_all($patt, $str, &$reg); $link = $reg[1]; // clean up array $link = cleanArray($link); if (count($link) > 0) $out .= "<i>See also:</i>\n"; for ($i=0; $i<count($link); $i++) { // added the line below to fix a second bug reported by <davor@croart.com> $link[$i] = ereg_replace("[[:space:]]+", " ", $link[$i]); // observed myself another bug with references to URLs - JMC // check if it is a HTTP URL or a crossrefence $protocols = "(http|https|ftp|telnet|gopher)://|(mailto|news):"; if (ereg($protocols, $link[$i])) { // parse the link and mark it using <> $prot1 = "^((mailto|news):.+)$"; $prot2 = "(http|https|ftp|telnet|gopher)://"; $prot2 = "^(.*) *\((".$prot2.".+)\)$"; if (ereg($prot1, $link[$i], &$regurl)) { list ($tmp, $url) = $regurl; $desc = $url; } elseif (ereg($prot2, $link[$i], &$regurl)) { list ($tmp, $desc, $url) = $regurl; if ($desc == "") $desc = $url; } $out .= "&lt;<a href=\"".chop($url)."\" target=\"_blank\">"; $out .= chop($desc)."</a>&gt; "; } else { $out .= "[<a href=\"".$PHP_SELF."?query_term="; $out .= urlencode($link[$i])."&database=".urlencode($db); $out .= "&strategy=exact\">".$link[$i]."</a>] "; if (($i % 5) == 0 && $i > 0) $out .= "\n"; } } $out .= "\n"; return $out; } function parr($arr) { echo "<ul>"; while (list($k,$v) = each($arr)) { if (gettype($v) == "array") { echo "<ul>"; echo "* $k , new array*<br>"; parr($v); echo "</ul>"; } else { echo "$k = $v<br>"; } } echo "</ul>"; } // perform a query to the server function doQuery($str, $db, $strategy) { global $PHP_SELF; $query = new DictQuery(); $query->define($str, $db); $n = $query->get("numres"); $res = $query->get("result"); $out = "<b>Found ".count($res); $out .= (count($res)==1) ? " hit" : " hits"; $out .= "</b> - <i>Term: ".$str.", Database: ".$db.", Strategy: ".$strategy; $out .= "</i><br>\n<dl>\n"; for ($i=0; $i<count($res); $i++) { $entry = $res[$i]; $out .= "<dt>[".($i + 1)."] : ".$entry["dbname"]."</dt>\n"; $out .= "<dd><pre>".mkLinks($entry["definition"], $db)."</pre></dd>\n"; } $out .= "</dl>"; return $out; } if ($query_term){ $out = doQuery($query_term, $database, $strategy); echo $out."\n<hr>\n"; } ?> Last accessed: <?php $end = time(); echo date("Y/m/d H:i:s",time()); echo " [Total processing time: ".($end - $start)." seconds]"; ?> </body> </html>