I'm new to coding and I have been searching for a script to create a list of Anagrams, the following script is what I am using but it will display a list of words made up of every possible combination.
I'm wanting to add a spell check to the function such as pspell_check() which I found here visit https://www.php.net/manual/en/function.pspell-check.php
The idea is to only display words from the English dictionary.
Any help would be appreciated if anyone can show me how to add spell check to this code.
<?php
// PHP program to print all
// permutations of a given string.
/**
* permutation function
* @param str string to
* calculate permutation for
* @param l starting index
* @param r end index
*/
function permute($str, $l, $r)
{
if ($l == $r)
echo $str. "\n";
else
{
for ($i = $l; $i <= $r; $i++)
{
$str = swap($str, $l, $i);
permute($str, $l + 1, $r);
$str = swap($str, $l, $i);
}
}
}
/**
* Swap Characters at position
* @param a string value
* @param i position 1
* @param j position 2
* @return swapped string
*/
function swap($a, $i, $j)
{
$temp;
$charArray = str_split($a);
$temp = $charArray[$i] ;
$charArray[$i] = $charArray[$j];
$charArray[$j] = $temp;
return implode($charArray);
}
// Driver Code
$str = "ANAGRAM";
$n = strlen($str);
permute($str, 0, $n - 1);
// This code is contributed by mits.
?>
<?php
// PHP program to print all
// permutations of a given string.
/**
* permutation function
* @param str string to
* calculate permutation for
* @param l starting index
* @param r end index
*/
function permute($str, $l, $r, $pspell_link)
{
if ($l == $r) {
if (pspell_check($pspell_link, $str)) {
echo $str. "\n";
};
}
else
{
for ($i = $l; $i <= $r; $i++)
{
$str = swap($str, $l, $i);
permute($str, $l + 1, $r, $pspell_link);
$str = swap($str, $l, $i);
}
}
}
/**
* Swap Characters at position
* @param a string value
* @param i position 1
* @param j position 2
* @return swapped string
*/
function swap($a, $i, $j)
{
$temp;
$charArray = str_split($a);
$temp = $charArray[$i] ;
$charArray[$i] = $charArray[$j];
$charArray[$j] = $temp;
return implode($charArray);
}
// Driver Code
$str = "ANAGRAM";
$n = strlen($str);
$pspell_link = pspell_new("en"); // pspell has to be enabled on your web server
permute($str, 0, $n - 1, $pspell_link);
// This code is contributed by mits.
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.