Jump to content

eregi TO preg_match help.


Canadian

Recommended Posts

I'm trying to collect a number from a form and test whether or not the number ends in 999.  The book I'm using to learn PHP it uses the ereg() or eregi() functions for regular expressions.  I've just learned that these were phased out in PHP 5.3 after receiving and error.  I think I have to use preg_match now.  Regardless, I'm having some trouble with the syntax.  Maybe it changed with preg_match.  My code and error is below... 

 

Thanks

 

 

Number Entry Form (just a basic form)...

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>Untitled Document</title>

</head>

 

<body>

 

<h1>Enter Your Lucky Number! Are You a Winner?</h1>

 

  <form action="entry_results.php" method="post">

    <table border="0">

      <tr>

        <td>Please Enter Your Favorite Number</td>

        <td><input type="text" name="entry" maxlength="13" size="13"></td>

      </tr>

    </table>

   

    <input type="submit" value="Submit" />

   

  </form>

 

 

</body>

</html>

 

 

 

Here is my Page that collects the $_POST variable and looks for a number ending in 999 (I'm using the $ sign at the end of the 999 in the preg_match() function because my books says this is how you look for a match at the end of a string using an ereg() function...

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

<title>Untitled Document</title>

</head>

 

<body>

 

 

<?php

 

$entry = $_POST['entry'];

 

if (preg_match('999$', $entry)) {

 

echo "You are a winner!!!";

}

 

else {

 

echo "Sorry, better luck next time.";

}

 

?>

 

 

</body>

</html>

 

 

Here is my error...

 

 

Warning: preg_match() [function.preg-match]: Delimiter must not be alphanumeric or backslash in /Applications/XAMPP/xamppfiles/htdocs/flocycling44php/iMillionz_test_files/entry_results.php on line 15

Sorry, better luck next time.

 

Link to comment
https://forums.phpfreaks.com/topic/191019-eregi-to-preg_match-help/
Share on other sites

You need to use a regex format:

 

<?php
$entry = "1230999";
   
   if (preg_match('/999$/', $entry)) {
   
      echo "You are a winner!!!";
   }
   
   else {
   
      echo "Sorry, better luck next time.";
   }
?>

 

See: http://us.php.net/manual/en/function.preg-match.php

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.