I'm currently trying to use PhP to "parse" user input. The page is just a form with one input field and the submit button.
The result I want to achieve is to make PhP echo(rand(0,100) if the user types "rand" But if the user types something of this form: "rand int1-int2" it echos "rand(int1,int2)"
I'm currently using switch, case, break for user input.
Thank you in advance !
<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;
}
}
?>
You can achieve this using 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;
}
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.