|
Spell Checking and URL Tricks
Now change the "bogus" part of the url to some other word. This is how this
was done:
In my Apache server configuration I have this:
<Location /s>
ForceType application/x-httpd-php3
</Location>
Then I created a file named simply 's' and placed it in my document root. The file
contains:
<html><head><title>Simple Spell-Checker</title></head>
<body>
<h3>Simple Spell-Checker</h3>
<?php
$aspell_link=aspell_new("english");
$word = substr($PATH_INFO,1); /* strip away first / */
if (aspell_check($aspell_link,$word)) {
echo "Correct spelling of '$word'<P>\n";
} else {
echo "Incorrect spelling of '$word'<P>\n";
}
echo "Possible correct spellings include:<br>\n";
$suggestions=aspell_suggest($aspell_link,$word);
for($i=0; $i < count($suggestions); $i++) {
echo "<li>" . $suggestions[$i] . "<br>";
}
echo "</ol>";
?>
</body></html>
The trick here is that by telling Apache that the /s resource is actually of MIME type
application/x-httpd-php3 which is the magical MIME type which triggers PHP, the s
file will be run like a PHP script. Any text in the URL following the /s will be put into the
$PATH_INFO variable. So in my script I just strip away the first slash and then treat the
remaining text as the word I want to spell check.
A couple of other variables get set to useful things on a request like this. Our /s/bogus request
from above results in the following:
SCRIPT_FILENAME = /home/httpd/html/s
REQUEST_URI = /s/bogus
SCRIPT_NAME = /s
PATH_INFO = /bogus
PATH_TRANSLATED = /home/httpd/html/bogus
You can of course discover this yourself by putting a <phpinfo()> tag in your
script and loading it up.
Now, for the spell checking part of this. There isn't much to it. You can read about
the functions used here:
http://www.phpbuilder.com/manual/ref.aspell.php
http://www.phpbuilder.com/manual/ref.aspell.php
If you don't have aspell enabled, download the aspell tarball and install it. Note that
version 0.27 does not work with PHP since the author did not include a C-callable library.
Version 0.26 works fine. The author has promised to bring back the C-callable library in a
future version.
-Rasmus


