OK, here it goes. I’m trying to combine two scripts i found to make a die roller that emails the roll results. I run an RPG site. I’m getting this parse error.
Parse error: syntax error, unexpected $end in C:\xampp\htdocs\diceroller\index.php on line 142
line 142 is actually one line after the end of the script. I’m really no good yet with PHP so please be gentle.
Here’s the code
[code] <?php
// PHP 5
class dice {
// format 1d20*13+14
function roll($input)
{
$math = array("+", "-", "*", "/");
// split the string into individual characters
$characters = str_split($input);
foreach($characters as $char)
{
if(!in_array($char, $math))
{
$die .= $char;
} else {
break;
}
}
$parts = explode("d", $die);
$num = $parts[0]; // number of dice to roll
$sides = $parts[1]; // number of sides on the dice
// to figure out equation
$extra = explode($char, $input);
// roll x number of die
for($i=1; $i <= $num; $i++)
{
// add to array
$rolls[$i] = rand(1, $sides);
}
// creates string of numbers
foreach($rolls as $roll)
{
$numbers .= $roll . ',';
}
// removes trailing , from string of numbers to form [ numbers ]
$numbers = substr($numbers, 0, -1);
// sum array - get total of all dice rolled
$total = array_sum($rolls);
// if statement solves error with calc_string method...
if(strlen($extra[1] >= 2))
{
// build equation string
$eq = $char . '' . $extra[1];
$f = $total . ' ' . $eq;
// compute final total
$final = $this->calc_string($f);
}else{
$final = $total;
}
echo '[' . $input . '] => ['.$numbers.'] = ' . $final . '<br>';
}
// thanks to http://www.gamedev.net/community/forums/topic.asp?topic_id=436613
// for following method to solve an equation within a string!
function calc_string( $mathString )
{
$cf_DoCalc = create_function("", "return (" . $mathString . ");" );
return $cf_DoCalc();
}
};
if(isset($_POST['submit']))
{
$times = $_POST['times'];
$dice = new dice();
for($i=1; $i <= $times; $i++)
{
$dice->roll($_POST['roll']);
}
}
?>
<?php
if ($_POST[“email”]<>’’) {
$ToEmail = $_POST[“sendto”];
$EmailSubject = 'Die roll from Role To Hit ';
$mailheader = “From: “.$_POST[“sentfrom”].”\r\n”;
$MESSAGE_BODY = “Name: “.$_POST[“roll”].”
”;
$MESSAGE_BODY .= “Comment: “.nl2br($_POST[“comments”]).”
”;
mail($ToEmail, $EmailSubject, $MESSAGE_BODY, $mailheader) or die (“Failure”);
?>
Your message was sent
<html>
<head>
<title>Roll Dice</title>
</head>
<body>
<form method="post" action="<?php echo $PHP_SELF; ?>">
<TABLE BORDER="0">
Max length is 120 characters )