These two method are more or less equivalent. The major difference is that eregi is automatically case insensitive and accepts no switches, therefore requires no switch to tell it to be so or anything else. I'm oversimplifying because this is likely all you need to know to make the conversion in the specific code you attached, you can look up these two methods at http://php.net/ to see other differences.
Now, because preg_match requires a switch to be case insensitive (and can have other switches for other things), we need some way of differentiating the expression we are using from the switch, ex:
PHP Code:
if(eregi(/\.jpg$/, 'image.JPG'))
will return true (pass on to the next command that depends upon its being true) because it's case insensitive and \.jpg$
matches the .JPG
in the string being tested. But with preg_match we need to be more specific:
PHP Code:
if(preg_match('/\.jpg$/i', 'image.JPG'))
See, we need extra delimiters so we can use the switch. This will also match (return true) because we told preg_match to be case insensitive.
So, besides changing every occurrence of eregi to preg_match in the code, we also have to change the regular expressions used. These appear to be:
PHP Code:
// email validation regular expression
$regex = "^[-a-z0-9!#$%&\'*+/=?^_`{|}~]+(\.[-a-z0-9!#$%&\'*+/=?^_`{|}~]+)*@(([a-z0-9]([-a-z0-9]*[a-z0-9]+)?){1,63}\.)+([a-z]([-a-z0-9]*[a-z0-9]+)?){2,63}$";
$header_injection_regex = "(\r|\n)";
Which I would make:
PHP Code:
// email validation regular expression
$regex = "<^[-a-z0-9!#$%&\'*+/=?^_`{|}~]+(\.[-a-z0-9!#$%&\'*+/=?^_`{|}~]+)*@(([a-z0-9]([-a-z0-9]*[a-z0-9]+)?){1,63}\.)+([a-z]([-a-z0-9]*[a-z0-9]+)?){2,63}$>i";
$header_injection_regex = "/(\r|\n)/i";
?>
which, because you're running low on possible delimiters for $regex
, I'm not certain will work, but in testing seems feasible. I used the <> characters as delimiters, and testing shows that should work (they're allowed as delimiters and are not in the $regex
expression). I will attach the entire updated file. We can try other combinations if necessary and/or troubleshoot any typos, I'm sure something will work.
form_for_forum_preg_match.zip
Bookmarks