Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated for windows and bash #5

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
9 changes: 7 additions & 2 deletions README.MD
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
### WARNING - THIS IS A HACKY UPDATE WITH DICTIONARY SUPPORT AND MODIFICATIONS TO THE COMMAND LINE - USE AT YUOR OWN RISK!

##Hunspell PHP wrapper
This is a hunspell php wrapper.

===================
### This fork changes the shell commands with windows and bash style shells. This will no longer work on the system the original author wrote it on. I'm not sure what type of shell they were using but the manner in which they were setting the environment variable does not work on windows or centos/bash. This fork addresses that problem by using the windows "set" command via powershell which supports piping. The command for non windows machines was changed to use "export" for setting the environment.

An additional change was made to the parsing of the return value as the `PHP_EOL` value used in the original source was not working on my machine. This was changed to "\n" and it works great.
===================


Example
===================
```php
Expand Down
25 changes: 20 additions & 5 deletions src/HunspellPHP/Hunspell.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,13 @@ protected function clear($input)
*/
protected function findCommand($input)
{
return shell_exec(sprintf("LANG=%s; echo '%s' | hunspell -d %s", $this->encoding, $input, $this->language));

if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
return shell_exec(sprintf("powershell \"set LANG='%s'; echo '%s' | hunspell -d %s\"", $this->encoding, $input, $this->language));
} else {
return shell_exec(sprintf("export LANG='%s'; echo '%s' | hunspell -d %s", $this->encoding, $input, $this->language));
}

}

/**
Expand All @@ -134,7 +140,13 @@ protected function findCommand($input)
*/
protected function stemCommand($input)
{
return shell_exec(sprintf("LANG=%s; echo '%s' | hunspell -d %s -s", $this->encoding, $input, $this->language));

if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
return shell_exec(sprintf("powershell \"set LANG='%s'; echo '%s' | hunspell -d %s -s\"", $this->encoding, $input, $this->language));
} else {
return shell_exec(sprintf("export LANG='%s'; echo '%s' | hunspell -d %s -s", $this->encoding, $input, $this->language));
}

}

/**
Expand All @@ -144,10 +156,13 @@ protected function stemCommand($input)
*/
protected function preParse($input, $words)
{
$result = explode(PHP_EOL, trim($input));
unset($result[0]);
$words = array_map('trim', explode(" ", $words));
$result = explode("\n", trim($input));
array_shift($result);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I forgot to mention in the notes that I also changed the split char from " " to a regex \W because when people search for things on my site like blue/black it chokes up. I also added a catch in case something else breaks a warning is not thown if vars of two sizes are going to be passed to array_combine.

$words = array_map('trim', preg_split('/\W/', $words));

if(sizeof($result) != sizeof($words)) {
return [];
}
return array_combine($words, $result);
}

Expand Down