473,385 Members | 1,587 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

php spellchecker

170 100+
Hi there,

I've just searched for 40 minutes (39 longer than normal) for some php spellchecker code that works exactly like the one in this text box (underlining misspelled words in red) as I type. Can anyone point me in the direction of one? Please?
Dec 7 '07 #1
5 1661
RuthC
33
Hi there,

I've just searched for 40 minutes (39 longer than normal) for some php spellchecker code that works exactly like the one in this text box (underlining misspelled words in red) as I type. Can anyone point me in the direction of one? Please?
Hi beary,
Try this..
http://chxo.com/scripts/spellcheck.php?showsource=1#source
Dec 7 '07 #2
beary
170 100+
Hi beary,
Try this..
http://chxo.com/scripts/spellcheck.php?showsource=1#source
Um, RuthC, what can I say, I am seriously in your debt! Thankyou. I found this one very early on in my searches, but read the stuff about aspell and assumed my server (at work where I need this) wouldn't have the requirements. But I've uploaded it and it all seems to work. You're a gem. Thanks again.
Dec 7 '07 #3
beary
170 100+
However, I've just discovered that it only seems to work on FF, not IE6. Surely this can't be right? Were you aware of this limitation? I've tested it on the same page using FF and IE6. Firefox, no problems. IE6, nothing works....
Dec 7 '07 #4
beary
170 100+
OK. Well is anyone out there able to explain why this code does not work in IE6? Thanks.

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. // 2002-10-10 by Chris Snyder
  3. // this script uses aspell, but not the internal php spelling functions which are somewhat crippled by only being able to check words, not whole documents (as of ~php 4.2.2)
  4. // requires that aspell be installed and working at /usr/local/bin/aspell -- see http://aspell.sourceforge.net/
  5.  
  6. // 2003-01-19 -- updated tempfile to use PHP's tempfile creation mech., also bumped year
  7. // 2003-01-24 -- fixed bug that caused improper handling of words with no suggested corrections (thanks, Dekeyzer Stephane!)
  8. // 2003-05-06 -- fixed a bug causing bad things to happen when multiple instances of incorrect words were found on any given line  (thanks, Dallas Brown!)
  9. //             -- also fixed script so that $opener and $closer work for custom labeling of errors (thanks again, Dallas Brown!)
  10.  
  11. /*
  12. spellcheck.php -- aspell-based spellchecker implemented in PHP
  13. Copyright (C) 2003 by Chris Snyder (csnyder@chxo.com)
  14.  
  15. This program is free software; you can redistribute it and/or
  16. modify it under the terms of the GNU General Public License
  17. as published by the Free Software Foundation; either version 2
  18. of the License, or (at your option) any later version.
  19.  
  20. This program is distributed in the hope that it will be useful,
  21. but WITHOUT ANY WARRANTY; without even the implied warranty of
  22. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23. GNU General Public License for more details.
  24.  
  25. You should have received a copy of the GNU General Public License
  26. along with this program; if not, write to the Free Software
  27. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  28. */
  29.  
  30. $text= $_POST['text'];
  31. $showsource= $_GET['showsource'];
  32.  
  33. //
  34. // file paths and the aspell command have moved down below line 58
  35. //
  36.  
  37. // show source code part I
  38. if ($showsource) $sourceinfo= "Script: $_SERVER[SCRIPT_FILENAME] ( <a href='#source'>source code</a> )";
  39. else $sourceinfo= "<a href='$_SERVER[SCRIPT_NAME]?showsource=1#source'>show PHP source code</a>";
  40. if (trim($text)!="") {
  41.     $showsource= 0;
  42.     $sourceinfo= "";
  43.     }
  44.  
  45. print "<html>
  46. <head>
  47. <title>$uripath</title>
  48. <style type='text/css'>
  49.     body { font-family: Verdana, Geneva, sans-serif; font-size: 12px; line-height: 18px; background-color: #ffffdd;}
  50.     table { font-family: Verdana, Geneva, sans-serif; font-size: 12px; }
  51.     .heading { font-size: 12px; font-weight: bold; background-color: #666677; color: #dddddd; border: 1px; border-style: solid; }
  52.     .oddrow { background-color: #ffffff; }
  53.     .evenrow { background-color: #eeffee; }
  54. </style>
  55. </head>
  56. <body>
  57. <h1>Spell Check Some Text</h1>
  58. <p>$sourceinfo</p>";
  59.  
  60. // if text+check is supplied, first open and create $temptext, then spell check
  61. if (trim($text)!="" && ($_POST['submit']=="check" || $_POST['submit']=="re-check")) {
  62.  
  63.     // HERE'S WHERE YOU MIGHT NEED TO CHANGE FILE PATHS, etc.
  64.     //
  65.     // set up some vars and create a tempfile
  66.     // tempnam() is a PHP function that creates a unique tempfile in the specified path,
  67.     //    with the specified prefix
  68.     $temptext= tempnam("/tmp", "spelltext");
  69.  
  70.     // if you spellcheck alot of HTML, add the -H flag to aspell to put it in SGML mode
  71.     $aspellcommand= "cat $temptext | /usr/local/bin/aspell -a";
  72.  
  73.     // these three determine how errors are flagged ($indicator is a description of what $opener and $closer do)
  74.     $indicator= "bold";
  75.     $opener= "<b>";
  76.     $closer= "</b>";
  77.     //
  78.     // END OF CONFIGURATION
  79.  
  80.  
  81.     if ($fd=fopen($temptext,"w")) {
  82.         $textarray= explode("\n",$text);
  83.         fwrite($fd,"!\n");
  84.         foreach($textarray as $key=>$value) {
  85.             // adding the carat to each line prevents the use of aspell commands within the text...
  86.             fwrite($fd,"^$value\n");
  87.             }
  88.         fclose($fd);
  89.  
  90.         // next create tempdict and temprepl (skipping for now...)
  91.  
  92.         // next run aspell
  93.         $return= shell_exec($aspellcommand);
  94.  
  95.     // now unlink that tempfile
  96.     $ureturn= unlink($temptext);
  97.  
  98.         //next parse $return and $text line by line, eh?
  99.         $returnarray= explode("\n",$return);
  100.         $returnlines= count($returnarray);
  101.         $textlines= count($textarray);
  102.  
  103.         //print "text has $textlines lines and return has $returnlines lines.";
  104.         $lineindex= -1;
  105.         $poscorrect= 0;
  106.         $counter= 0;
  107.         foreach($returnarray as $key=>$value) {
  108.             // if there is a correction here, processes it, else move the $textarray pointer to the next line
  109.             if (substr($value,0,1)=="&") {
  110.                 //print "Line $lineindex correction:".$value."<br>";
  111.                 $correction= explode(" ",$value);
  112.                 $word= $correction[1];
  113.                 $absposition= substr($correction[3],0,-1)-1;
  114.                 $position= $absposition+$poscorrect;
  115.                 $niceposition= $lineindex.",".$absposition;
  116.                 $suggstart= strpos($value,":")+2;
  117.                 $suggestions= substr($value,$suggstart);
  118.                 $suggestionarray= explode(", ",$suggestions);
  119.                 //print "I found <b>$word</b> at $position. Will suggest $suggestions.<br>";
  120.  
  121.                 // highlight in text
  122.                 $beforeword= substr($textarray[$lineindex],0,$position);
  123.                 $afterword= substr($textarray[$lineindex],$position+strlen($word));
  124.                 $textarray[$lineindex]= $beforeword."$opener$word$closer".$afterword;
  125.  
  126.                 // kludge for multiple words in one line ("<b></b>" adds 7 chars to subsequent positions, for instance)
  127.                 $poscorrect= $poscorrect+strlen("$opener$closer");
  128.  
  129.                 // build the correction form
  130.                 $counter= $counter+1;
  131.                 $formbody.= "<tr>
  132.                                 <td align='right'>$word</td>
  133.                                 <td>
  134.                                     <input type='hidden' name='position$counter' value='$niceposition'>
  135.                                     <input type='hidden' name='incorrect$counter' value=\"$word\">
  136.                                     <select name='suggest$counter' onChange=\"document.corrector.correct$counter.value=this.value;\">
  137.                                     <option value=\"$word\" selected>$word (as-is)</option>
  138.                                     ";
  139.                 foreach ($suggestionarray as $key=>$value) {
  140.                     $formbody.= "<option value=\"$value\">$value</option>
  141.                                 ";
  142.                     }
  143.                 $inputlen= strlen($word)+5;
  144.                 $formbody.= "<option value=''>custom:</option>
  145.                                     </select>
  146.                                     <input type='text' name='correct$counter' value=\"$word\" size='$inputlen'>
  147.                                 </td>
  148.                               </tr>";
  149.                 }
  150.  
  151.             elseif (substr($value,0,1)=="#") {
  152.                 //print "Line $lineindex unknown:".$value."<br>";
  153.                 $correction= explode(" ",$value);
  154.                 $word= $correction[1];
  155.                 $absposition= $correction[2] - 1;
  156.                 $position= $absposition+$poscorrect;
  157.                 $niceposition= $lineindex.",".$absposition;
  158.                 $suggestions= "no suggestions";
  159.                 $suggestionarray= explode(", ",$suggestions);
  160.                 //print "I found <b>$word</b> at $position. Will suggest $suggestions.<br>";
  161.  
  162.                 // highlight in text
  163.                 $beforeword= substr($textarray[$lineindex],0,$position);
  164.                 $afterword= substr($textarray[$lineindex],$position+strlen($word));
  165.                 $textarray[$lineindex]= $beforeword."$opener$word$closer".$afterword;
  166.  
  167.                 // kludge for multiple words in one line ("<b></b>" adds 7 chars to subsequent positions)
  168.                 $poscorrect= $poscorrect+strlen("$opener$closer");
  169.  
  170.                 // build the correction form
  171.                 $counter= $counter+1;
  172.                 $formbody.= "<tr>
  173.                                 <td align='right'>$word</td>
  174.                                 <td>
  175.                                     <input type='hidden' name='position$counter' value='$niceposition'>
  176.                                     <input type='hidden' name='incorrect$counter' value=\"$word\">
  177.                                     <select name='suggest$counter' onChange=\"document.corrector.correct$counter.value=this.value;\">
  178.                                     <option value=\"$word\" selected>$word (as-is)</option>
  179.                                     ";
  180.                 $inputlen= strlen($word)+3;
  181.                 $formbody.= "<option value=''>custom:</option>
  182.                                     </select>
  183.                                     <input type='text' name='correct$counter' value=\"$word\" size='$inputlen'>
  184.                                 </td>
  185.                               </tr>";
  186.                 }
  187.  
  188.             else {
  189.                 //print "Done with line $lineindex, next line...<br><br>";
  190.                 $poscorrect=0;
  191.                 $lineindex= $lineindex+1;
  192.                 }
  193.             }
  194.         }
  195.     print "<hr>Uncorrected Text (potential errors in $opener$indicator$closer):<blockquote>";
  196.     foreach ($textarray as $key=>$value) {
  197.         print $value."<br>";
  198.         }
  199.     print "</b><!-- comment catcher --></blockquote>";
  200.  
  201.     $htmltext= htmlentities($text);
  202.     if ($formbody=="") $formbody= "<tr><td>&nbsp;</td><td><br><b>No errors!</b><br>Click 'correct' to continue with text unchanged.<br>&nbsp;</td></tr>";
  203.     print "<hr><h3>Correction form:</h3>
  204.     <form name='corrector' action='spellcheck.php' method='post'>
  205.     <input type='hidden' name='text' value=\"$htmltext\">
  206.     <table>
  207.     $formbody
  208.     <tr>
  209.       <td>&nbsp;</td>
  210.       <td><input type='submit' name='submit' value='correct'>
  211.           <input type='reset' name='reset' value='reset form'>
  212.       </td>
  213.     </tr>
  214.     </table>
  215.     </form>";
  216.  
  217.     //print "<hr>Return:".nl2br($return);
  218.     }
  219.  
  220. // or if text+correct is specified, make the indicated corrections
  221. elseif (trim($text)!="" && $_POST['submit']=="correct") {
  222.     $textarray= explode("\n",$text);
  223.  
  224.     $index= 1;
  225.     $lastlineindex= 0;
  226.     $poscorrect= 0;
  227.  
  228.     // look through list of positions and make corrections
  229.     while (isset($_POST["position$index"])) {
  230.         $positionarray= explode(",",$_POST["position$index"]);
  231.         $lineindex= $positionarray[0];
  232.         $absposition= $positionarray[1];
  233.  
  234.         if ($lastlineindex==$lineindex) {
  235.             $position= $absposition+$poscorrect;
  236.             }
  237.         else {
  238.             $poscorrect= 0;
  239.             $position= $absposition;
  240.             }
  241.         $lastlineindex= $lineindex;   
  242.         $correct= $_POST["correct$index"];
  243.         $incorrect= $_POST["incorrect$index"];
  244.         //print "Found correction at $lineindex,$absposition. Replacing ";
  245.  
  246.         $before= substr($textarray[$lineindex],0,$position);
  247.         $after= substr($textarray[$lineindex],$position+strlen($incorrect));
  248.         $textarray[$lineindex]= $before.$correct.$after;
  249.  
  250.         $poscorrect= (strlen($correct)-strlen($incorrect))+$poscorrect;
  251.         //print "Position correction is now $poscorrect.<br>";
  252.         $index= $index+1;
  253.         }
  254.  
  255.     //print "Original text:<br>";
  256.     //print nl2br($text);
  257.     //print "<hr>";
  258.  
  259.     foreach ($textarray as $key=>$value) {
  260.         $newtext.=$value;
  261.         }
  262.     print "
  263.     <form action='spellcheck.php' method='post'>
  264.         <h3>Your Corrected Text:</h3><br>
  265.         <textarea name='text' cols='60' rows='10'>$newtext</textarea><br>
  266.         <input type='submit' name='submit' value='re-check'> | <a href='spellcheck.php'>Clear/Restart</a> | <a href='spellcheck.php?showsource=1#source'>Show Source</a>
  267.     </form>";
  268.     }
  269.  
  270. // otherwise, show the initial form
  271. else {
  272.     print "
  273.     <form action='spellcheck.php' method='post'>
  274.         Text to Check:<br>
  275.         <textarea name='text' cols='60' rows='10'>$newtext</textarea><br>
  276.         <input type='submit' name='submit' value='check'>
  277.     </form>";
  278.     }
  279.  
  280. // show source code part II
  281. if ($showsource) {
  282.     print "<hr><a name='source'> </a><h1>PHP Source:</h1>";
  283.     $void= show_source($_SERVER['SCRIPT_FILENAME']);
  284.     }
  285.  
  286. print "
  287. <hr>
  288. spellcheck.php Copyright (C) 2003 by Chris Snyder<br>
  289. This program comes with ABSOLUTELY NO WARRANTY.  This is free software, and you are welcome
  290. to redistribute it under certain conditions; please refer to the
  291. <a href='http://www.gnu.org/licenses/gpl.html'>GNU General Public License</a> for details.
  292. </body>
  293. </html>";
  294. ?>
  295.  
Dec 9 '07 #5
beary
170 100+
OK. Well is anyone out there able to explain why this code (link below) does not work in IE6? Thanks.

http://chxo.com/scripts/spellcheck.php?showsource=1#source
Dec 9 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Alex Perlov | last post by:
Let's say I have a string s; and I know exactly what allocator string s is using. Then I have separately allocated buffer char *buf = allocator<char>::allocate(buflen); which is initialized...
1
by: Jim Douglas | last post by:
I have (3) legacy ASP applications where I have been tasked with implementing a SpellChecker in, the catch is that the SpellChecker has to be done in .NET. I have some ideas about how to do it but...
1
by: frazer | last post by:
hi i want to include word's spell check feature in my app. how do i do that? thnx
2
by: Lady_A | last post by:
I have created a basic COM in-proc server and a client. The registration of my server succeeds according to regsvr32. I can see it in the registry, having the ProgID and the InProcServer32...
1
by: corrigal | last post by:
I am using Visual Studion 2005 with VB and ASP developing a web application. I have automated MSWord to use the spellchecker against a textbox I have on a page. In the V.S. development mode the...
0
by: Armen Kirakosyan | last post by:
HI all !! have an appication which should spell check BUT ! I cannot dynamicaly change language, or set to autodetect the server in English, but I want it spell check in German or Spanish here...
0
by: Armen Kirakosyan | last post by:
HI all !! have an appication which should spell check BUT ! I cannot dynamicaly change language, or set to autodetect the server in English, but I want it spell check in German or Spanish here...
2
by: Dean Slindee | last post by:
Using this statement to utilize the spell checker in WinWord: WinOffice.clsWord.SpellChecker(txtNote.Text) 'this does not work because no main window is displayed 'Dim proc =...
17
by: malathib | last post by:
Hi, I have used a rich text editor in my application. I got it from one of the site.all are JS files. My problem is, i want to call a javascript function from the iframe. Can anybody help...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.