Home | Linux |     Share This Page
Dictionary front end using PHP
All content Copyright © 2006, P. Lutus

Use this PHP script to access a local or Internet "dict" server on a Linux system.
Copy the section between the horizontal lines, save it to a text file with the suffix ".php", and place it in your local Web server page set.

<html>
<head>
<title>
Dictionary search using "dict"
</title>
<style type="text/css">
A { text-decoration:none; }
A:hover { color: #800080; font-weight:bold; }
input {
	border: 1px solid black;
	padding-right:4px;
	padding-left:4px;
}
</style>
</head>
<body>
<script language="JavaScript">
function processLink(s)
{
	s = s.replace(/\s+/g," ");
	location.href=self.name + "?query=" + escape(s);
}
</script>
<form name="form1">
Enter word for "dict" search: <input value="<?php
	$query = $_REQUEST['query'];
	# clean up passed value
	$query = preg_replace("/^\s+/","",$query);
	$query = preg_replace("/\s+$/","",$query);
	$query = preg_replace("/\s+/"," ",$query);
	$value = preg_replace("/\"/","&quot;",$query);
	echo "$value"
?>" type="text" name="query" onChange="submit()"><p>
</form>
<?php
if($query != "") {
	$equery = escapeshellarg($query);
	exec("/usr/bin/dict $equery 2>&1",$output,$error);
	$output = implode("\n",$output);
	if($error) {
		echo "<pre><b>Error: $output<b></pre>";
	}
	else {
		if (preg_match("/no definitions found/i",$output)) {
			# turn suggested alternatives into links
			$output = preg_replace("/(: +)(\w+)/",
				"\\1<a href=\"javascript:processLink('\\2');\">\\2</a>",$output);
			echo "<pre>$output</pre>";
		}
		else {
			# bold first line
			$output = preg_replace("/^(.*)/","<b>\\1</b>",$output);
			# wrap first line of each reference in table to control background color
			$output = preg_replace("/(\n\nFrom )(.*)\n/","\n\n<table cellpadding=4
				bgcolor=#d0d0ff><tr><td><b>\\2</b></td></tr></table>",$output);
			# find and process document internal links
			$output = preg_replace("/\{+(.*?)\}+/s",
				"<a href=\"javascript:processLink('\\1');\">\\1</a>",$output);
			echo "<pre>$output</pre>";
		}
	}
}
?>
<script language="JavaScript">
	document.form1.query.focus();
</script>
</body>
</html>
        
 

Home | Linux |     Share This Page