Log in

View Full Version : Resolved regex php 5.3 error



ggalan
06-22-2011, 08:59 PM
php 5.3 gives an error

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

when i test this is php 5.2, i have no problem
i know that ereg is deprecated so i switched it to preg_match for 5.3


$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++)
{
if (!ereg("^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$",
$local_array[$i]))
{
return false;
}
}


but in 5.3 i get an error


$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++)
{
if (!preg_match("/^(([A-Za-z0-9!#$%&'*+/=?^_`{|}~-][A-Za-z0-9!#$%&'*+/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$/",
$local_array[$i]))

{
return false;
}
}


any ideas?

bluewalrus
06-22-2011, 09:32 PM
I think it is


if (!preg_match("/^(([A-Za-z0-9!#$%&'*+/<------- that should be \/

The forward (are they back? I know the symbol not the phrasing hah) slashes are reserved for the opening and closing of the expression you need to escape them anywhere else you use them in your regular expression with a back (maybe forward?) slash (\).

Your going to have to do that in 2 places...

traq
06-22-2011, 10:16 PM
you can actually use any character you like to enclose the regex, as long as you escape it wherever it appears in the pattern. the slash ( / ) is fairly standard. As bluewalrus says, anyplace you have a slash, you need to escape it (so it is treated as a literal character) by placing a backslash immediately before it.

ggalan
06-22-2011, 11:05 PM
so forward slashes are delimiters /
and i would need to \/ escape the forward slash to treat it literal?

bluewalrus
06-22-2011, 11:19 PM
That is correct.

ggalan
06-22-2011, 11:47 PM
i thought regex was independent of php version, wonder why php 5.2 didnt throw an error

djr33
06-23-2011, 03:50 AM
Regex isn't necessarily standardized. It's implemented in PHP based on standards, but there may be slight variations. (This isn't an official explanation, just what I believe based on what I've seen.)
My guess is that there is a slight variation in how the particular characters are parsed in the versions. In fact, it might even be a bug in one version or the other. Basically one version doesn't assume a special character in that context and the other does.
It would be interesting to see if the new version works in the older PHP version as well. Does it?

traq
06-23-2011, 04:42 AM
the old ereg functions didn't use delimiters for the regex pattern, so this problem wouldn't have come up. it's really a php parsing issue, not a regex issue per se. there are many implementations of regular expressions - php uses PCRE (perl-compatible regex).

more about delimiters in php (http://www.php.net/manual/en/regexp.reference.delimiters.php) ( <--surprisingly, one of the more comprehensible pages in the manual )

jimmybrion
06-23-2011, 10:36 AM
Thanks for google and dynamicdrive for help me to fix this issue