0

我有以下代码:

<?php
if (!isset($_SESSION["word"])) {
    $dictionary_file = new SplFileObject("dictionary.txt");
    $dictionary_file->seek(rand(0, 80367));
    $_SESSION["word"] = $dictionary_file->current();
    $_SESSION["word_progress"] = "";

    for ($i = 0; $i < strlen($_SESSION["word"]); $i++) {
        $_SESSION["word_progress"] .= "_";
    }
    echo strlen($_SESSION["word"]);
    echo $_SESSION["word"];
}
else {
    if (isset($_GET["guess"])) {
        // If their guessed letter is in the word
        if (stripos($_SESSION["word"], $_GET["guess"]) !== false) {
            $occurrence_points = strposall($_SESSION["word"], $_GET["guess"]);
            $progress = $_SESSION["word_progress"];

            foreach ($occurrence_points as $values) {
                $progress[$values] = $_GET["guess"];
            }

            $_SESSION["word_progress"] = $progress;
        }
    }
}

?>

我从文件中随机生成一个单词。说它产生“泡沫”。长度是6,但是打印出来确认字确实是气泡后还是会报8。为什么是这样?

4

1 回答 1

1

随机生成的单词可能会添加空格。做一个修剪()

   $_SESSION["word"] = trim($dictionary_file->current());
于 2013-02-16T22:38:29.670 回答