1

I have written a basic HTML/PHP to calculate the Cayley table of some also basic binary operations over a finite set with n elements.

The filename is q.php and the code (cleaned of all styles, etc.) is the following:

<!doctype html>
<html>
<head><meta http-equiv="content-type" content="text/html;"></head>
<body>
<form action="q.php" method="post">
    Order:&nbsp;<input type="number" name="c1"><br><br>
    Operation:&nbsp;<input type="text" name="c2"><br>
    (syntax:<strong>&nbsp;$i "+" $j "%" 3&nbsp;</strong>)<br><br>
    <input type="submit" name="submit" value="Calculate / Reset">
</form>
<?php
$n=$_POST['c1']; $p=$_POST['c2'];
echo "<table style=\"text-align: center; margin: auto;\" cellpadding=\"8\">\n";
for ($i=0; $i<$n; $i++) {
    echo "<tr>\n";
    for ($j=0; $j<$n; $j++) {
        eval("\$a=\"$p\";");
        echo "\t<td>".$a."</td>\n";
    }
    echo "</tr>\n";
}
echo "</table>\n";
?>
</body>
</html>

It is very easy to input the integer n in the form, but (exactly as here) I can't find a good way to input in the form the binary operators (are they strings?).

More precisely, if the operation on i and j is e.g. i+(j mod 3), I would like to input sometinhg like

i + j % 3

or even

$i + $j % 3

instead of

$i "+" $j "%" 3

This latter with double quotes - not really enjoyable for the user - works already.

Can we do something better? And maybe also manage brackets (), to calculate (i+j)*2 as easily as i+(j*2) that I already do?

I'm now curious, thank you!

Community
  • 1
  • 1
MattAllegro
  • 6,455
  • 5
  • 45
  • 52
  • I think you're looking for [`htmlspecialchars()`](http://htmlspecialchars). – Amal Murali Apr 22 '14 at 18:46
  • 2
    You had better really trust your users if you're going to `eval()` arbitrary data entered on a web form. – cpilko Apr 22 '14 at 18:50
  • @AmalMurali: are you sure this is the same? Doesn't it convert -e.g.- % to % ? – MattAllegro Apr 22 '14 at 18:58
  • 1
    @MattAllegro: Watch what happens if you type `print_r(phpinfo(),true)` into your input box. Check out [this SO question](http://stackoverflow.com/questions/1015242/how-to-evaluate-formula-passed-as-string-in-php) for some possible solutions. – cpilko Apr 22 '14 at 19:02
  • 1
    If you want to print the result of your post data you can use this `eval("\$a= {$p};"); echo "\t" . $p . " = " . $a . "\n";` in your code in the replacement of your eval code. – Gildonei Apr 22 '14 at 19:39
  • @gildonei: indeed, that's what I was tuning now. (Why) did you delete the answer? It solves the task. Tomorrow I'll also check the link to the SO question in comment by cpilko, but this one of you runs smooth! – MattAllegro Apr 22 '14 at 19:50
  • @MattAllegro sorry for post delete I didn't understand your question properly, so i thought I had posted a wrong answer. Undeleting post. – Gildonei Apr 22 '14 at 22:09

1 Answers1

1

Try this

<?php
$n = $_POST['c1']; 
$p = trim(str_replace(' ', '', $_POST['c2'])); // Remove spaces


eval("\$result = {$p};");
echo $result;
?>
Gildonei
  • 476
  • 5
  • 13