dongqishun6409 2013-09-09 04:38
浏览 44
已采纳

为什么PHP不能识别两个相等的字符串?

I'm working on a php function that will compare the components of two arrays. Each value in the arrays are only one english word long. No spaces. No characters.

Array #1: a list of the most commonly used words in the english language. $common_words_array

Array #2: a user-generated sentence, converted to lowercase, stripped of punctuation, and exploded() using the space (" ") as a delimiter. $study_array

There's also a $com_study array, which is used in this case to keep track of the order of commonly used words which get replaced in the $study_array by a "_" character.

Using nested for loops, what SHOULD happen is that the script should compare each value in Array #2 to each value in Array #1. When it finds a match (aka. a commonly used english word), it will do some other magic that's irrelevant to the current problem.

As of right now, PHP doesn't recognize when two array string values are equivalent. I'm adding in the code to the problematic function here for reference. I've added in a lot of unnecessary echo commands in order to localize the problem to the if statement.

Can anybody see something that I've missed? The same algorithm worked perfectly in Python.

function create_question($study_array, $com_study, $common_words_array)
{
for ($study=0; $study<count($study_array); $study++)
    {
    echo count($study_array)." total in study_array<br>";
    echo "study is ".$study."<br>";

    for ($common=0; $common<count($common_words_array); $common++)
        {
        echo count($common_words_array)." total in common_words_array<br>";
        echo "common is ".$common."<br>";

        echo "-----<br>";
        echo $study_array[$study]." is the study list word<br>";
        echo $common_words_array[$common]." is the common word<br>";
        echo "-----<br>";

          // The issue happens right here.
          if ($study_array[$study] == $common_words_array[$common])
            {
            array_push($com_study, $study_array[$study]);
            $study_array[$study] = "_";
            print_r($com_study);
            print_r($study_array);
            }
        }
    }

    $create_question_return_array = array();
    $create_question_return_array[0] = $study_array;
    $create_question_return_array[1] = $com_study;

    return $create_question_return_array;
}

EDIT: At the suggestion of you amazing coders, I've updated the if statement to be much more simple for purposes of debugging. See below. Still having the same issue of not activating the if statement.

if (strcmp($study_array[$study],$common_words_array[$common])==0)
{
echo "match found";
//array_push($com_study, $study_array[$study]);
//$study_array[$study] = "_";
//print_r($com_study);
//print_r($study_array);
}

EDIT: At bansi's request, here's the main interface snippet where I'm calling the function.

$testarray = array();

$string = "This is a string";
$testarray = create_study_string_array($string);

$testarray = create_question($testarray, $matching, $common_words_array);

As for the result, I'm just getting a blank screen. I would expect to have the simplified echo statement output "match found" to the screen, but that's not happening.

  • 写回答

5条回答 默认 最新

  • ds0409 2013-09-09 04:51
    关注

    (EDIT) make sure your that your splitting function removes excess whitespace (e.g. preg_split("\\s+", $input)) and that the input is normalized properly (lowercase'd, special chars stripped out, etc.).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败