<?php
// pspell_test.php
// RJM Programming
// September, 2024
// Thanks to https://www.php.net/manual/en/function.pspell-check.php
$results='';
$prefix='';
$midbit='';
$lb='';
$suffix='';
$cstring=" ... This is a valid spelling";
$wstring=" ... Sorry, wrong spelling";
if (isset($_GET['testwords']) || isset($_POST['testwords'])) {
$pspell = pspell_new("en");
$sentence=str_replace("’","'",('' . trim("" . (isset($_GET['testwords']) ? str_replace('+',' ',urldecode($_GET['testwords'])) : '') . (isset($_POST['testwords']) ? str_replace('+',' ',urldecode($_POST['testwords'])) : ''))) . '');
$origwords=explode(' ',trim($sentence));
$sentence=str_replace("’","'",str_replace('~','',str_replace('?~','',str_replace('!~','',str_replace('.~','',('' . trim("" . (isset($_GET['testwords']) ? str_replace('+',' ',urldecode($_GET['testwords'])) : '') . (isset($_POST['testwords']) ? str_replace('+',' ',urldecode($_POST['testwords'])) : ''))) . '~')))));
$words=explode(' ',trim($sentence));
$finalwords=explode(' ',trim($sentence));
$fwords=explode(' ',trim($sentence));
if (sizeof($words) > 1) { 
  $results='<table style="border:1px dotted pink;"><tr><td>'; 
  $suffix='</td></tr></table>'; 
  $lb='<br>'; 
  $midbit='</td><td>'; 
  $cstring='&#10004;'; 
  $wstring='&#10060;';
}

for ($jj=0; $jj<sizeof($words); $jj++) {
$finalwords[$jj]='';
$fwords[$jj]='';
for ($kk=0; $kk<strlen($words[$jj]); $kk++) {
  if (substr(substr($words[$jj],$kk),0,1) >= '0' && substr(substr($words[$jj],$kk),0,1) <= '9') {
    $finalwords[$jj].=substr(substr($words[$jj],$kk),0,1);
  } else if (strtolower(substr(substr($words[$jj],$kk),0,1)) >= 'a' && strtolower(substr(substr($words[$jj],$kk),0,1)) <= 'z') {
    $finalwords[$jj].=substr(substr($words[$jj],$kk),0,1);
    $fwords[$jj].=substr(substr($words[$jj],$kk),0,1);
  } else if (substr(substr($words[$jj],$kk),0,1) == '-') {
    $finalwords[$jj].=substr(substr($words[$jj],$kk),0,1);
    $fwords[$jj].=substr(substr($words[$jj],$kk),0,1);
  } else if (substr(substr($words[$jj],$kk),0,1) == "'" && $kk != 0 && $kk != (-1 + strlen($words[$jj]))) {
    $finalwords[$jj].=substr(substr($words[$jj],$kk),0,1);
    $fwords[$jj].=substr(substr($words[$jj],$kk),0,1);
  }
}
if (pspell_check($pspell,  $finalwords[$jj]     ) || $fwords[$jj] == '' || is_numeric('' . $fwords[$jj])) {
    $results.="" . $origwords[$jj] . $lb . $cstring;
} else {
    $results.="" . $origwords[$jj] . $lb . $wstring;
    $suggestions = pspell_suggest($pspell, "" . $words[$jj]);
    $results.="<br><br>";
    $ii=0;
    foreach ($suggestions as $suggestion) {
        if (strpos($results, '<select') === false) {
          $ii=2;
          $results.='<select size=2><option value="">Possible spelling ...</option></select>';
        } else {
          $ii++;
        } 
        $results=str_replace('</select>', '<option value="' . $suggestion . '">' . $suggestion . '</option></select>', $results);
    }
    if ($ii > 0) {
        $results=str_replace('<select', '<select onclick="if (this.size != ' . $ii . ') { this.size=' . $ii . ';  }" ', $results);
    }
}
$results=str_replace('</select>', '</SELECT>', str_replace('<select', '<SELECT', $results));
$results.=$midbit;
}
$results.=$suffix;
}
echo "<html>
<head>
<style>
  td {
    vertical-align: top;
  }
</style>
<title>Try out Pspell - RJM Programming - September, 2024</title>
</head>
<body>
<h1>Try out Pspell for English</h1>
<h3>RJM Programming - September, 2024</h3>
<h4>Thanks to <a target=_blank href='//www.php.net/manual/en/function.pspell-check.php'>php.net</a></h4>
<div id=results>" . $results . "</div><br>
<br>
<form action=./pspell_test.php method=POST>
<textarea style=width:80%; data-type=text name=testwords id=words placeholder='Type in your word(s) to check the spelling, regarding ...' value=''></textarea>
<br><br><input type=submit value=Check style=background-color:yellow;></input>
</form>
</body>
</html>";
?>
