0

Possible Duplicate:
Function ereg() is deprecated

I understand that in the new version of PHP ereg is deprecated:

Deprecated: Function ereg() is deprecated

The code below worked fine in the old PHP. So I replaced ereg with preg_match and I am now getting the following error:

Warning: preg_match() [function.preg-match]: Unknown modifier '{' in

Here is my code...

if (!empty($searchval))
{
    if(ereg("[0-9]{5}", $searchval)) {
        $zip = $searchval;
    }
    else {
        $city = $searchval;
    }
} else
{
    $error .= "<div id='Error'>Please enter zip</div>";
    $zip = false;
}
Community
  • 1
  • 1
Greg
  • 1,025
  • 4
  • 13
  • 30

4 Answers4

2

preg_*() need a delimiter for the regex.

In most cases, / is used, but you may use anything, so long as they match. ~ and @ are common, because they are unlikely to appear in most regexes.

alex
  • 460,746
  • 196
  • 858
  • 974
2
if(preg_match("~\d{5}~", $searchval)) {

Left code stays the same

zerkms
  • 240,587
  • 65
  • 429
  • 525
2

You need to surround the regular expression with a delimiter like:

if(preg_match("/[0-9]{5}/", $searchval)) {
Brett Zamir
  • 13,309
  • 6
  • 48
  • 73
1

You haven't included a delimiter in your regular expression. Try this:

if (!empty($searchval))
{
    if(ereg("/[0-9]{5}/", $searchval)) {
    $zip = $searchval;
    }
    else {
    $city = $searchval;
    }
} else
{
    $error .= "<div id='Error'>Please enter zip</div>";
    $zip = false;
}
Crashspeeder
  • 4,181
  • 2
  • 15
  • 21