Jump to content

Php Calculator Changing Ereg To Preg_Match?


Recommended Posts

Hey guys, I recently posted here and I am now trying to fix the ereg because I was told it was deprecated I read some of the stuff in the php manual however I don't understand most of it since I am extremely new to programming. Here's my code, which works fine when i use the ereg function but when i use preg_match() it doesnt work it gives always give me the output "please restrict your input to numbers." However it works just fine when I use !ereg. If i enter numbers it gives me an answer and I enter letters it gives me the "please restrict your input to numbers." message. Here's the code.

 

<HTML>
<HEAD>
<TITLE> Grade calculator </TITLE>
</HEAD>
<center><h1 style="Font-family:Vijaya;font-size:34;Color:#556B2F;"> Grade Calculator </h1></center>
<BODY bgcolor="#9BCD9B">


<?php
$submit = $_POST["submit"];
$Lab1 = $_POST["Lab1"];
$Lab2 = $_POST["Lab2"];
$Lab3 = $_POST["Lab3"];
$LA1 = $_POST["LA1"];
$LA2 = $_POST["LA2"];
$LA3 = $_POST["LA3"];
$LA4 = $_POST["LA4"];
$Quiz1 = $_POST["Quiz1"];
$Quiz2 = $_POST["Quiz2"];
$Quiz3 = $_POST["Quiz3"];
$Quiz4 = $_POST["Quiz4"];
$Final = $_POST["Final"];

if ($submit)   
{


    if (($Lab1 == "") || ($Lab2 =="") || ($Lab3 =="") || ($LA1 =="") || ($LA2 =="") || ($LA3 =="") || ($LA4 =="") || ($Quiz1 =="") || ($Quiz2 =="") || ($Quiz3 =="") || ($Quiz4 =="") || ($Final ==""))
     {
     echo("<font face=\"Tahoma\" size=\"2\" color=\"#FF0000\"><b>You Left an area blank, please try again.</b></font><br>");
     }


    elseif ( ( !preg_match("[0-9]",$Lab1) ) || ( !preg_match("[0-9]",$Lab2) ) || ( !preg_match("[0-9]",$Lab3)) || ( !preg_match("[0-9]",$LA1))|| ( !preg_match("[0-9]",$LA2))|| ( !preg_match("[0-9]",$LA3)) || ( !preg_match("[0-9]",$LA4))||( !preg_match("[0-9]",$Quiz1))|| ( !preg_match("[0-9]",$Quiz2)) || ( !preg_match("[0-9]",$Quiz3))||( !preg_match("[0-9]",$Quiz4)||( !preg_match("[0-9]",$Final))))
     {
     echo("<font face=\"Tahoma\" size=\"2\" color=\"#FF0000\"><b>Please restrict your input to numbers.</b></font><br>");
     }

else
     {   
     $LabTotal=($Lab1 + $Lab2 + $Lab3)*(.08);
     $LAtotal=($LA1+$LA2+$LA3+$LA4)*(.06);
     $Quiztotal=($Quiz1+$Quiz2+$Quiz3+$Quiz4)*(.2);
     $Finalscore=($Final)*(.36);
     $FinalGrade=($Finalscore+$Quiztotal+$LAtotal+$LabTotal);




if ($FinalGrade> 93 and $FinalGrade <=1000)
 {
  print("your grade is: " . $FinalGrade . "% " . "A");
 }
 elseif ($FinalGrade>88 and $FinalGrade<=93)
 {
  print("your grade is: " . $FinalGrade . "% " . "A-");
 }
 elseif ($FinalGrade>84 and $FinalGrade<=88)
 {
  print("your grade is: " . $FinalGrade . "% " . "B+");
 }
 elseif ($FinalGrade>79 and $FinalGrade<=84)
 {
  print("your grade is: " . $FinalGrade . "% " . "B");
 }
 elseif ($FinalGrade>76 and $FinalGrade<=79)
 {
  print("your grade is: " . $FinalGrade . "% " . "B-");
 }
 elseif ($FinalGrade>72 and $FinalGrade <=76)
 {
  print("your grade is: " . $FinalGrade . "% " . "C+");
 }
 elseif ($FinalGrade>69 and $FinalGrade <=72)
 {
  print("your grade is: " . $FinalGrade . "% " . "C");
 }
 elseif ($FinalGrade>63 and $FinalGrade <=69)
 {
  print("your grade is: " . $FinalGrade . "% " . "C-");
 }
 elseif ($FinalGrade>59 and $FinalGrade <=63)
 {
  print("your grade is: " . $FinalGrade . "% " . "D+");
 }
 elseif ($FinalGrade>54 and $FinalGrade<=59)
 {
  print("your grade is: " . $FinalGrade . "% " . "D");
 }
 elseif ($FinalGrade>50 and $FinalGrade<=53)
 {
  print("your grade is: " . $FinalGrade . "% " . "D-");
 }
 elseif ($FinalGrade<49)
 {
  print("your grade is: " . $FinalGrade . "% " . "F");
 }
  }
}
?>
<FORM METHOD="post" ACTION="test.php">
<P><font face="Tahoma" size="2"><b>Enter your Lab Scores: <br><INPUT TYPE="text" NAME="Lab1" SIZE=3></b></font></p>
<font face="Tahoma" size="2"><b><INPUT TYPE="text" NAME="Lab2" SIZE=3></b></font></p>

<font face="Tahoma" size="2"><b><INPUT TYPE="text" NAME="Lab3" SIZE=3></b></font></p>
<font face="Tahoma" size="2"><b>Enter your Lecture Assignment Scores: <br><INPUT TYPE="text" NAME="LA1" SIZE=3></b></font></p>
<font face="Tahoma" size="2"><INPUT TYPE="text" NAME="LA2" SIZE=3></b></font></p>
<font face="Tahoma" size="2"><INPUT TYPE="text" NAME="LA3" SIZE=3></b></font></p>
<font face="Tahoma" size="2"><INPUT TYPE="text" NAME="LA4" SIZE=3></b></font></p>
<font face="Tahoma" size="2"><b>Enter your Quiz Scores: <br><INPUT TYPE="text" NAME="Quiz1" SIZE=3></b></font></p>
<font face="Tahoma" size="2"><INPUT TYPE="text" NAME="Quiz2" SIZE=3></b></font></p>
<font face="Tahoma" size="2"><INPUT TYPE="text" NAME="Quiz3" SIZE=3></b></font></p>
<font face="Tahoma" size="2"><INPUT TYPE="text" NAME="Quiz4" SIZE=3></b></font></p>
<font face="Tahoma" size="2"><b>Enter your Final Score: <br><INPUT TYPE="text" NAME="Final" SIZE=3></b></font></p>
</b></font>
<P><INPUT TYPE="submit" NAME="submit" VALUE="Evaluate Grade"></p>
</FORM>
</BODY>
</HTML>
</HTML>
</html>

Edited by Nerock

You're missing the delimiters around the ReExps. In short, add a "/" after the opening quote and before the closing quote. Which should make the RegExp look like this:

"/[0-9]/"

 

For more information, I can highly recommend Regular-Expressions.info. It contains all the information you'll ever need on Regular Expression.

 

PS: You can use \d instead of the [0-9] group, means the same.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.