草庐IT

PHP similar_text() 和 in_array() 没有像他们应该的那样工作

coder 2024-04-17 原文

我正在尝试使用 similar_text()in_array() 在 PHP 中制作一个简单的拼写检查和建议程序。 我有一个文本文件 dictionary.txt,其中包含英语中的大部分单词。

首先,我将文本文件中的所有单词都换行放入一个数组中。然后在用户输入和提交时,我使用 in_array() 检查他们输入的单词是否在数组中。如果是,那么他们拼写正确。

如果不是,则我使用 similar_text() 在数组中查找与拼写错误的单词接近的单词。

我遇到了两个无法解决的问题,我相信我正在根据 PHP 文档正确使用 in_array()similar_text() .

第一个问题是,当用户键入并提交文本文件中应该也应该在数组中的单词时,else 会触发,这不应该发生。因为它在文本文件中,所以它应该在数组中,并且 in_array() 应该评估为真。

第二个问题是我收到一个错误,我通过 similar_text() 存储两个词之间相似度百分比的变量未定义。 我正在使用它,similar_text(),就像在文档注释示例中一样;事实上,我在每次比较之前重置和重新定义 $percentageSimilarity。为什么我会收到未定义的错误消息?

这是我的代码:

<?php
function addTo($line){
    return $line;
}
$words = array_map('addTo', file('dictionary.txt'));
if(isset($_GET['checkSpelling'])){
    $input = (string)$_GET['checkSpelling'];
    $suggestions = array();
    if(in_array($input, $words)){
        echo "you spelled the word right!";
    }
    else{
        foreach($words as $word){
            $percentageSimilarity=0.0;
            similar_text($input, $word, $percentageSimilarity);
            if($percentageSimilarity>=95){
                 array_push($suggestions, $word);
            }
         }
         echo "Looks like you spelled that wrong. Here are some suggestions: \n";
         foreach($suggestions as $suggestion){
             echo $suggestion;
         }
     }
  }
  ?>
  <!Doctype HTMl>
 <html lang="en">
     <head>
          <meta charset="utf-8" />
         <title>Spell Check</title>
     </head>
     <body>
         <form method="get">
             <input type="text" name="checkSpelling" autocomplete="off" autofocus />
         </form>
     </body>
 </html>

最佳答案

将您的添加行更改为

function addTo($line){
    return strtolower(trim($line));
}

并将您的输入更改为

$input = strtolower(trim($_GET['checkSpelling']));

文件命令有一个讨厌的习惯,它会留下尾随的换行符,所以你可能不会基于那个进行匹配……trim 应该处理这个问题。其他更改只是为了使其不区分大小写。

关于PHP similar_text() 和 in_array() 没有像他们应该的那样工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16910116/

有关PHP similar_text() 和 in_array() 没有像他们应该的那样工作的更多相关文章

  1. ruby-on-rails - rails : "missing partial" when calling 'render' in RSpec test - 2

    我正在尝试测试是否存在表单。我是Rails新手。我的new.html.erb_spec.rb文件的内容是:require'spec_helper'describe"messages/new.html.erb"doit"shouldrendertheform"dorender'/messages/new.html.erb'reponse.shouldhave_form_putting_to(@message)with_submit_buttonendendView本身,new.html.erb,有代码:当我运行rspec时,它失败了:1)messages/new.html.erbshou

  2. ruby-on-rails - 由于 "wkhtmltopdf",PDFKIT 显然无法正常工作 - 2

    我在从html页面生成PDF时遇到问题。我正在使用PDFkit。在安装它的过程中,我注意到我需要wkhtmltopdf。所以我也安装了它。我做了PDFkit的文档所说的一切......现在我在尝试加载PDF时遇到了这个错误。这里是错误:commandfailed:"/usr/local/bin/wkhtmltopdf""--margin-right""0.75in""--page-size""Letter""--margin-top""0.75in""--margin-bottom""0.75in""--encoding""UTF-8""--margin-left""0.75in""-

  3. ruby - 在 Ruby 中实现 `call_user_func_array` - 2

    我怎样才能完成http://php.net/manual/en/function.call-user-func-array.php在ruby中?所以我可以这样做:classAppdeffoo(a,b)putsa+benddefbarargs=[1,2]App.send(:foo,args)#doesn'tworkApp.send(:foo,args[0],args[1])#doeswork,butdoesnotscaleendend 最佳答案 尝试分解数组App.send(:foo,*args)

  4. ruby-on-rails - 'compass watch' 是如何工作的/它是如何与 rails 一起使用的 - 2

    我在我的项目目录中完成了compasscreate.和compassinitrails。几个问题:我已将我的.sass文件放在public/stylesheets中。这是放置它们的正确位置吗?当我运行compasswatch时,它不会自动编译这些.sass文件。我必须手动指定文件:compasswatchpublic/stylesheets/myfile.sass等。如何让它自动运行?文件ie.css、print.css和screen.css已放在stylesheets/compiled。如何在编译后不让它们重新出现的情况下删除它们?我自己编译的.sass文件编译成compiled/t

  5. ruby - 难道Lua没有和Ruby的method_missing相媲美的东西吗? - 2

    我好像记得Lua有类似Ruby的method_missing的东西。还是我记错了? 最佳答案 表的metatable的__index和__newindex可以用于与Ruby的method_missing相同的效果。 关于ruby-难道Lua没有和Ruby的method_missing相媲美的东西吗?,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/7732154/

  6. ruby-on-rails - Rails 源代码 : initialize hash in a weird way? - 2

    在rails源中:https://github.com/rails/rails/blob/master/activesupport/lib/active_support/lazy_load_hooks.rb可以看到以下内容@load_hooks=Hash.new{|h,k|h[k]=[]}在IRB中,它只是初始化一个空哈希。和做有什么区别@load_hooks=Hash.new 最佳答案 查看rubydocumentationforHashnew→new_hashclicktotogglesourcenew(obj)→new_has

  7. ruby-on-rails - Rails 3 I18 : translation missing: da. datetime.distance_in_words.about_x_hours - 2

    我看到这个错误:translationmissing:da.datetime.distance_in_words.about_x_hours我的语言环境文件:http://pastie.org/2944890我的看法:我已将其添加到我的application.rb中:config.i18n.load_path+=Dir[Rails.root.join('my','locales','*.{rb,yml}').to_s]config.i18n.default_locale=:da如果我删除I18配置,帮助程序会处理英语。更新:我在config/enviorments/devolpment

  8. ruby - 检查 "command"的输出应该包含 NilClass 的意外崩溃 - 2

    为了将Cucumber用于命令行脚本,我按照提供的说明安装了arubagem。它在我的Gemfile中,我可以验证是否安装了正确的版本并且我已经包含了require'aruba/cucumber'在'features/env.rb'中为了确保它能正常工作,我写了以下场景:@announceScenario:Testingcucumber/arubaGivenablankslateThentheoutputfrom"ls-la"shouldcontain"drw"假设事情应该失败。它确实失败了,但失败的原因是错误的:@announceScenario:Testingcucumber/ar

  9. ruby-on-rails - rails 目前在重启后没有安装 - 2

    我有一个奇怪的问题:我在rvm上安装了ruby​​onrails。一切正常,我可以创建项目。但是在我输入“railsnew”时重新启动后,我有“程序'rails'当前未安装。”。SystemUbuntu12.04ruby-v"1.9.3p194"gemlistactionmailer(3.2.5)actionpack(3.2.5)activemodel(3.2.5)activerecord(3.2.5)activeresource(3.2.5)activesupport(3.2.5)arel(3.0.2)builder(3.0.0)bundler(1.1.4)coffee-rails(

  10. Ruby Koans about_array_assignment - 非平行与平行分配歧视 - 2

    通过ruby​​koans.com,我在about_array_assignment.rb中遇到了这两段代码你怎么知道第一个是非并行赋值,第二个是一个变量的并行赋值?在我看来,除了命名差异之外,代码几乎完全相同。4deftest_non_parallel_assignment5names=["John","Smith"]6assert_equal["John","Smith"],names7end45deftest_parallel_assignment_with_one_variable46first_name,=["John","Smith"]47assert_equal'John

随机推荐