Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP spell checking tool

Is there such a tool that finds language/spelling errors in code comment and strings in PHP code? for example,

<?php
$myVar = "Hollo World";
//this is a code commont with spelling error
/*this is anothor wrong comment*/
?>

If I run such a tool, then it will find 'Hollo', 'commont', and 'anothor' spelling errors for me.

like image 944
Beier Avatar asked Nov 30 '22 05:11

Beier


2 Answers

Take a look at the PHP function pspell_check() which is part of Pspell.

It requires the Aspell library.

You might also be interested in Enchant, the PHP binding for the Enchant Library. It supports Aspell, and in the words of the documentation:

Enchant steps in to provide uniformity and conformity on top of all spelling libraries, and implement certain features that may be lacking in any individual provider library.


Here's a pspell_check() example from the documentation. First you link to the appropriate dictionary, then you perform the spell check:

<?php
$pspell_link = pspell_new("en");

if (pspell_check($pspell_link, "testt")) 
{
    echo "This is a valid spelling";
} else 
{
    echo "Sorry, wrong spelling";
}
?>

// Output is "Sorry, wrong spelling"

To check spelling in an entire file (like all code and comments in a program), you could transform the file to a string using file(), strip punctuation with preg_replace(), break it into words with explode(), and run it through the spell check.


Since your question is tagged PHP, I assume you would like a PHP oriented programmatic solution; however, there are, of course, myriads of spell check options outside of PHP as well.

like image 127
Peter Ajtai Avatar answered Dec 09 '22 23:12

Peter Ajtai


IDEs such as Eclipse or NetBeans would do the spell checking themselves, you need only to enable such features.

like image 37
Tomasz Kowalczyk Avatar answered Dec 09 '22 23:12

Tomasz Kowalczyk