使用substr进行PHP表单验证

时间:2015-09-16 14:39:32

标签: php validation substr

我正在尝试创建一个接受字符串的程序,即使它拼写错误,只要前两个字母是正确的。早些时候我运行我的程序它工作正常,但当我把它

if(substr($player1, 0, 2) === "rock" || substr($player2, 0, 2) === "rock")  

然后点击提交它将不再打印。我的代码怎么办?或者应该使用什么功能?

<html>
<body>
<h1>ROCK PAPER SCISSORS</h1>
<?php


print ('<form action="" method="post">');
print ('<p>Player 1: <input type="text" name="p1" /></p>');
print ('<p>Player 2: <input type="text" name="p2" /></p>');
print ('<input type="submit" name="submit" value="PLAY" />');
print ('</form>');

if (isset($_POST['submit'])) {
    $player1score = 0;
    $player2score = 0;
    $draw = 0;
    $player1 = strtolower($_POST['p1']);
    $player2 = strtolower($_POST['p2']);

    if(substr($player1, 0, 2) === "rock" || substr($player2, 0, 2) === "rock"){

            if ($player1 == 'scissors' && $player2 == 'scissors') {
            $draw++;
            print "Player 1: $player1score<br>";
            print "Player 2: $player2score<br>";
            print "DRAW: $draw";
    }

            else if ($player1 == 'rock' && $player2 == 'rock') {
            $draw++;
            print "Player 1: $player1score<br>";
            print "Player 2: $player2score<br>";
            print "DRAW: $draw";
    }

            else if ($player1 == 'paper' && $player2 == 'paper') {
            $draw++;
            print "Player 1: $player1score<br>";
            print "Player 2: $player2score<br>";
            print "DRAW: $draw";
    }

            else if ($player1 =='rock' && $player2 =='scissors') {
            $player1score++;
            print "Player 1: $player1score<br>";
            print "Player 2: $player2score<br>";
            print "DRAW: $draw";
    }

            else if ($player1 == 'rock' && $player2 =='paper') {
            $player2score++;
            print "Player 1: $player1score<br>";
            print "Player 2: $player2score<br>";
            print "DRAW: $draw";

    }

        else if($player1 == 'scissors' && $player2 == 'rock') {
            $player2score++;
            print "Player 1: $player1score<br>";
            print "Player 2: $player2score<br>";
            print "DRAW: $draw";

    }

        else if ($player1 =='scissors' && $player2 =='paper') {
            $player1score++;
            print "Player 1: $player1score<br>";
            print "Player 2: $player2score<br>";
            print "DRAW: $draw";

    }
        else if ($player1 =='paper' && $player2 =='rock') {
             $player1score++;
            print "Player 1: $player1score<br>";
            print "Player 2: $player2score<br>";
            print "DRAW: $draw";

    }

        else if ($player1 =='paper' && $player2 == 'scissors') {
            $player2score++;
            print "Player 1: $player1score<br>";
            print "Player 2: $player2score<br>";
            print "DRAW: $draw";
    }
}
}
?>
</html>
</body>

1 个答案:

答案 0 :(得分:1)

问题出在字符串'rock'中,因为substr只返回2个字符,而不是4个字符。

这是你应该做的:

// Checking for "rock", but only care about the beginning of "ro" ck
if(substr($player1, 0, 2) === "ro" || substr($player2, 0, 2) === "ro")

我希望有所帮助。