【问题标题】:Using Microsoft Word's spell / grammar check with php用 php 使用 Microsoft Word 的拼写/语法检查
【发布时间】:2015-10-26 12:31:36
【问题描述】:

我从 1 天开始搜索是否有任何方法可以使用 php 中的 Microsoft 拼写检查和语法检查。我尝试使用 dotnet 类和 Microsoft.Office.Interop.Word 但未能使用单词拼写检查器。有什么建议吗?

【问题讨论】:

    标签: php spell-checking


    【解决方案1】:

    我无法对此进行测试,但这是发布在 PHP COM 函数页面上的示例,您尝试过吗?

    <?php
    function SpellCheck($input)
    {
        $word=new COM("word.application") or die("Cannot create Word object");
        $word->Visible=false;
        $word->WindowState=2;
        $word->DisplayAlerts=false;
        $doc=$word->Documents->Add();
        $doc->Content=$input;
        $doc->CheckSpelling();
        $result= $doc->SpellingErrors->Count;
        if($result!=0)
        {
            echo "Input text contains misspelled words.\n\n";
            for($i=1; $i <= $result; $i++)
            {       
                echo "Original Word: " .$doc->SpellingErrors[$i]->Text."\n";
                $list=$doc->SpellingErrors[$i]->GetSpellingSuggestions();
                echo "Suggestions: ";
                for($j=1; $j <= $list->Count; $j++)
                {
                    $correct=$list->Item($j);
                    echo $correct->Name.",";
                }
                echo "\n\n";
            }
        }
        else
        {
            echo "No spelling mistakes found.";
        }
        $word->ActiveDocument->Close(false);
        $word->Quit();
        $word->Release();
        $word=null;
    }
    
    $str="Hellu world. There is a spellling error in this sentence.";
    SpellCheck($str);
    ?>
    

    来源:PHP: COM Functions

    【讨论】:

    • 感谢 James Hunt,但是当我尝试运行代码时,我遇到了错误 Uncaught exception 'com_exception' 和消息 'Failed to create COM object `word.application'
    • 系统是否安装了COM对象这个词? - microsoft.com/en-us/download/…
    • 当我尝试使用 word.application 的 com 以显示它工作正常的版本时,但是当我尝试使用上述代码时,页面会连续加载而不会显示任何错误或结果。
    • 得到了解决方案,当我在 ms office 2010 中尝试它时,它实际上在 ms office 2007 中无法正常工作。谢谢 James
    • 没问题,如果这个答案是正确的,介意为其他用户点击吗?万一这个问题发生在别人身上。
    猜你喜欢
    • 2010-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-13
    • 2011-01-31
    • 2010-11-17
    • 1970-01-01
    • 2014-05-28
    相关资源
    最近更新 更多