Results 1 to 9 of 9

Thread: regex php 5.3 error

  1. #1
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default regex php 5.3 error

    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
    Code:
    $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
    Code:
    $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?
    Last edited by ggalan; 06-23-2011 at 01:51 PM.

  2. #2
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    I think it is

    PHP Code:
    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...
    Corrections to my coding/thoughts welcome.

  3. The Following User Says Thank You to bluewalrus For This Useful Post:

    ggalan (06-22-2011)

  4. #3
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    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.

  5. The Following User Says Thank You to traq For This Useful Post:

    ggalan (06-22-2011)

  6. #4
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default

    so forward slashes are delimiters /
    and i would need to \/ escape the forward slash to treat it literal?

  7. #5
    Join Date
    May 2007
    Location
    Boston,ma
    Posts
    2,127
    Thanks
    173
    Thanked 207 Times in 205 Posts

    Default

    That is correct.
    Corrections to my coding/thoughts welcome.

  8. #6
    Join Date
    Jan 2008
    Posts
    441
    Thanks
    67
    Thanked 4 Times in 4 Posts

    Default

    i thought regex was independent of php version, wonder why php 5.2 didnt throw an error

  9. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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?
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  10. #8
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    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 ( <--surprisingly, one of the more comprehensible pages in the manual )

  11. #9
    Join Date
    May 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    Thanks for google and dynamicdrive for help me to fix this issue

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •