0

私は現在、PhPを使用してユーザー入力を「解析」しようとしています。このページは、1つの入力フィールドと送信ボタンを備えた単なるフォームです。

私が達成したい結果は、ユーザーが「rand」と入力した場合にPhP echo(rand(0,100))を作成することですが、ユーザーが次の形式の何かを入力した場合:「randint1-int2」は「rand(int1、int2)」をエコーし​​ます

現在、ユーザー入力にswitch、case、breakを使用しています。

前もって感謝します !

<form action="<?php $_SERVER['PHP_SELF'] ?>" method="POST">
    <input type="text" name="commande" />
    <input type="submit" name="submit" value="envoyer!" />
</form>

<?php if (isset($_POST['commande'])) {

    switch($_POST['commande']){ 
        case "hello":
            echo"<h1> Hello </h1>";
        break;
        case substr($_POST['commande'], 0, 4)=="rand":
            echo(rand(1,100));
        break;
        }

    }
?>
4

1 に答える 1

1

これは、を使用して実現できますexplode

<?php
    $input = 'rand 1245';
    list($command, $arguments) = explode(' ', $input);
    $arguments = explode('-', $arguments);
    switch($command) {
        case 'rand':
            print_r($arguments);break;
            $min = 0;
            $max = 100;
            if (count($arguments) == 2) {
                $min = (int)$arguments[0];
                $max = (int)$arguments[1];
            }

            echo rand($min, $max);
            break;

    }
?>

実例

于 2012-04-09T15:25:54.053 に答える