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,045
  • 4
  • 14
  • 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
  • 479,566
  • 201
  • 878
  • 984
2
if(preg_match("~\d{5}~", $searchval)) {

Left code stays the same

zerkms
  • 249,484
  • 69
  • 436
  • 539
2

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

if(preg_match("/[0-9]{5}/", $searchval)) {
Brett Zamir
  • 14,034
  • 6
  • 54
  • 77
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,291
  • 2
  • 16
  • 21