PHP Classes

File: extendedsearch.php

Recommend this page to a friend!
  Classes of Jesus M. Castagnetto   Dictionary client class package   extendedsearch.php   Download  
File: extendedsearch.php
Role: ???
Content type: text/plain
Description: Example: Autogenerates a form allowing for more complex searches using the information provided by the server
Class: Dictionary client class package
Client dictionary class
Author: By
Last change:
Date: 23 years ago
Size: 6,750 bytes
 

Contents

Class file image Download
<!-- Extended 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>Extended autogenerated 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> <?php /* * I will autogenerate the form in the code below * You might want to make a static form to avoid * the overhead of the on-the-fly HTML code generation * and the query to the DICT server */ // functions to make the form function mkSelect($name, $nopt, $options, $default) { $out = "<select name=\"".$name."\">\n"; list($defkey,$defval) = each($default); $out .= "<option value=\"".$defkey."\" selected>".$defval."</option>\n"; while (list($key,$val) = each($options)) { if ($key != $defkey) $out .= "<option value=\"".$key."\">".$val."</option>\n"; } $out .= "</select>\n"; return $out; } function printForm($s) { $ndbs = $s->get_info("num_dbs"); $dbs = $s->get_info("databases"); $nstrats = $s->get_info("num_strats"); $strategies = $s->get_info("strategies"); $out = "<BR>Use database:<BR>\n"; $out .= mkSelect("database", $ndbs, $dbs, array("*"=>"Any")); $out .= "<BR>Type of Search:<BR>\n"; $out .= mkSelect("strategy", $ndbs, $strategies, array("exact"=>"Exact Search")); echo $out; } // get non-extended info from the server $server = new DictServerInfo(); // print the form printForm($server); ?> <br> <input type="submit" name="submit" value=" Search "> <input type="reset" name="reset" value=" Clear form input "> </form> <hr> <?php /* * If there was a query, this part will show the result */ // 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; } // Perform a query to the server function doQuery($str, $db, $strategy) { global $PHP_SELF; $query = new DictQuery(); if ($strategy == "exact") { $query->define($str, $db); } else { $query->match($str, $strategy, $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]; if ($strategy == "exact") { $out .= "<dt>[".($i + 1)."] : ".$entry["dbname"]."</dt>\n"; $out .= "<dd><pre>".mkLinks($entry["definition"], $db)."</pre></dd>\n"; } else { $match = explode(" ",chop($entry)); $match_term = str_replace("\"", "", $match[1]); $out .= "<dt>[". ($i + 1 ) . "] : "; $out .= "<A HREF=\"".$PHP_SELF."?query_term=".urlencode($match_term); $out .= "&database=".urlencode($db); $out .= "&strategy=exact\">"; $out .= $match_term."</a></dt>\n"; $out .= "<dd> Database: ".$match[0]."</dd>"; } } $out .= "</dl>"; return $out; } // if there was a query ... 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>