如何为php函数添加拼写检查

我是编码新手,我一直在寻找一个脚本来创建 Anagrams 列表,我正在使用以下脚本,但它将显示由每个可能的组合组成的单词列表。


我想向函数添加拼写检查,例如我在此处找到的 pspell_check() 访问https://www.php.net/manual/en/function.pspell-check.php


这个想法是只显示英语词典中的单词。


如果有人能告诉我如何向此代码添加拼写检查,将不胜感激。


<?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. 

?>


小唯快跑啊
浏览 133回答 1
1回答

慕神8447489

<?php&nbsp;// PHP program to print all&nbsp;&nbsp;// permutations of a given string.&nbsp;/**&nbsp;* permutation function&nbsp;* @param str string to&nbsp;&nbsp;*&nbsp; calculate permutation for&nbsp;* @param l starting index&nbsp;* @param r end index&nbsp;*/function permute($str, $l, $r, $pspell_link)&nbsp;{&nbsp;&nbsp; &nbsp; if ($l == $r) {&nbsp; &nbsp; &nbsp; &nbsp; if (pspell_check($pspell_link, $str)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo $str. "\n";&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; };&nbsp; &nbsp; }&nbsp; &nbsp; else&nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; for ($i = $l; $i <= $r; $i++)&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $str = swap($str, $l, $i);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; permute($str, $l + 1, $r, $pspell_link);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $str = swap($str, $l, $i);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; }&nbsp;}&nbsp;/**&nbsp;* Swap Characters at position&nbsp;* @param a string value&nbsp;* @param i position 1&nbsp;* @param j position 2&nbsp;* @return swapped string&nbsp;*/function swap($a, $i, $j)&nbsp;{&nbsp;&nbsp; &nbsp; $temp;&nbsp;&nbsp; &nbsp; $charArray = str_split($a);&nbsp;&nbsp; &nbsp; $temp = $charArray[$i] ;&nbsp;&nbsp; &nbsp; $charArray[$i] = $charArray[$j];&nbsp;&nbsp; &nbsp; $charArray[$j] = $temp;&nbsp;&nbsp; &nbsp; return implode($charArray);&nbsp;}&nbsp;// Driver Code&nbsp;$str = "ANAGRAM";&nbsp;$n = strlen($str);$pspell_link = pspell_new("en"); // pspell&nbsp; has to be enabled on your web serverpermute($str, 0, $n - 1, $pspell_link);&nbsp;// This code is contributed by mits.&nbsp;?>
打开App,查看更多内容
随时随地看视频慕课网APP