PHP Regex problem

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8398
  • Loc: USA

Post 3+ Months Ago

I can't find a decent working REGEX to verify EMAIL and URL...

Any one I use give me an "Unknown modifier x" problem or some other kind of sh*t. I google for what I need, and every example I use produces those types of errors... any help here?

Right now I'm using:

For Email:
Code: [ Select ]
#([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)#


For URL:
Code: [ Select ]
#(http://|)(www\.)?([^\.]+)\.(\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))#


Bot those fail to validate even the correctly filled ones.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8398
  • Loc: USA

Post 3+ Months Ago

Right now, I changed my EMAIL regex to:
Code: [ Select ]
/(?:[a-zA-Z0-9_\'\^\&\/\+\-])+(?:\.(?:[a-zA-Z0-9_\'\^\&\/\+\-])+)*@(?:(?:\[?(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\.){3}(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\]?)|(?:[a-zA-Z0-9-]+\.)+(?:[a-zA-Z]){2,}\.?)/

That works well on a different PHP file test that I put up... the test is as goes:
PHP Code: [ Select ]
<pre><?php
   $code = "user@domain.com
<!-- e -->user.subuser@domain.com<!-- e -->
<!-- e -->.user@domain.com<!-- e -->
<!-- e -->user.@domain.com<!-- e -->
<!-- e -->elitezenithweb@gmail.com<!-- e -->
@domain.com
user@domain
<!-- e -->user.subuser@domain.subdomain.com<!-- e -->
<!-- e -->user@domain.org<!-- e -->
<!-- e -->user@domain.info<!-- e -->
<!-- e -->user@domain.biz<!-- e -->
user@
@
user
domain
.
com";
   $regex = "/(?:[a-zA-Z0-9_\'\^\&\/\+\-])+(?:\.(?:[a-zA-Z0-9_\'\^\&\/\+\-])+)*@(?:(?:\[?(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\.){3}(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\]?)|(?:[a-zA-Z0-9-]+\.)+(?:[a-zA-Z]){2,}\.?)/";
   preg_match_all($regex, $code, $matches);
   
   print_r($matches);
?></pre>
  1. <pre><?php
  2.    $code = "user@domain.com
  3. <!-- e -->user.subuser@domain.com<!-- e -->
  4. <!-- e -->.user@domain.com<!-- e -->
  5. <!-- e -->user.@domain.com<!-- e -->
  6. <!-- e -->elitezenithweb@gmail.com<!-- e -->
  7. @domain.com
  8. user@domain
  9. <!-- e -->user.subuser@domain.subdomain.com<!-- e -->
  10. <!-- e -->user@domain.org<!-- e -->
  11. <!-- e -->user@domain.info<!-- e -->
  12. <!-- e -->user@domain.biz<!-- e -->
  13. user@
  14. @
  15. user
  16. domain
  17. .
  18. com";
  19.    $regex = "/(?:[a-zA-Z0-9_\'\^\&\/\+\-])+(?:\.(?:[a-zA-Z0-9_\'\^\&\/\+\-])+)*@(?:(?:\[?(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\.){3}(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\]?)|(?:[a-zA-Z0-9-]+\.)+(?:[a-zA-Z]){2,}\.?)/";
  20.    preg_match_all($regex, $code, $matches);
  21.    
  22.    print_r($matches);
  23. ?></pre>

I get the following from that:
Code: [ Select ]
Array
(
    [0] => Array
        (
            [0] => user@domain.com
            [1] => user.subuser@domain.com
            [2] => user@domain.com
            [3] => elitezenithweb@gmail.com
            [4] => user.subuser@domain.subdomain.com
            [5] => user@domain.org
            [6] => user@domain.info
            [7] => user@domain.biz
        )

)
  1. Array
  2. (
  3.     [0] => Array
  4.         (
  5.             [0] => user@domain.com
  6.             [1] => user.subuser@domain.com
  7.             [2] => user@domain.com
  8.             [3] => elitezenithweb@gmail.com
  9.             [4] => user.subuser@domain.subdomain.com
  10.             [5] => user@domain.org
  11.             [6] => user@domain.info
  12.             [7] => user@domain.biz
  13.         )
  14. )

The problem though, is that REGEX doesn't work on my site... I am using it like this:

globals.php
Code: [ Select ]
$regex = array(
    'EMAIL' => "/(?:[a-zA-Z0-9_\'\^\&\/\+\-])+(?:\.(?:[a-zA-Z0-9_\'\^\&\/\+\-])+)*@(?:(?:\[?(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\.){3}(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\]?)|(?:[a-zA-Z0-9-]+\.)+(?:[a-zA-Z]){2,}\.?)/",
    'URL' => "/(http|s:\/\/[A-Za-z0-9_]+.[A-Za-z0-9]{2,5})/"
);
  1. $regex = array(
  2.     'EMAIL' => "/(?:[a-zA-Z0-9_\'\^\&\/\+\-])+(?:\.(?:[a-zA-Z0-9_\'\^\&\/\+\-])+)*@(?:(?:\[?(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))\.){3}(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\]?)|(?:[a-zA-Z0-9-]+\.)+(?:[a-zA-Z]){2,}\.?)/",
  3.     'URL' => "/(http|s:\/\/[A-Za-z0-9_]+.[A-Za-z0-9]{2,5})/"
  4. );


reviews.php
Code: [ Select ]
function submit_rrequest($name, $email, $web, $notes)
{
    global $db, $regex;
    
    $error = 0;
    
    if(empty($name))
    {
        ++$error;
        die($name);
    }
    
    if(!preg_match($regex['EMAIL'], $web))
    {
        ++$error;
        die($email);
    }
    
    if(!preg_match($regex['URL'], $web))
    {
        ++$error;
        die($web);
    }
// other code here
}
  1. function submit_rrequest($name, $email, $web, $notes)
  2. {
  3.     global $db, $regex;
  4.     
  5.     $error = 0;
  6.     
  7.     if(empty($name))
  8.     {
  9.         ++$error;
  10.         die($name);
  11.     }
  12.     
  13.     if(!preg_match($regex['EMAIL'], $web))
  14.     {
  15.         ++$error;
  16.         die($email);
  17.     }
  18.     
  19.     if(!preg_match($regex['URL'], $web))
  20.     {
  21.         ++$error;
  22.         die($web);
  23.     }
  24. // other code here
  25. }

The script always dies and prints my EMAIL, telling me that my email isn't being validated correctly... but if I switch:

die($email);

...with...

die($regex['EMAIL']);

...the regex is printed... what is the deal here? Why can't I get it to work here?
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

I think preg_match is particular about it's parameter types, but I could be way off - just a shot in the dark. Have you tried saving that value to a var and then using that var instead of the array call?

Code: [ Select ]
#     $email = $regex['EMAIL'];
#     if(!preg_match($email, $web))
#     {
#         ++$error;
#         die($email);
#     }
#     $url = $regex['URL'];
#     if(!preg_match($url, $web))
#     {
#         ++$error;
#         die($web);
#     }
  1. #     $email = $regex['EMAIL'];
  2. #     if(!preg_match($email, $web))
  3. #     {
  4. #         ++$error;
  5. #         die($email);
  6. #     }
  7. #     $url = $regex['URL'];
  8. #     if(!preg_match($url, $web))
  9. #     {
  10. #         ++$error;
  11. #         die($web);
  12. #     }
  • Bogey
  • Genius
  • Genius
  • Bogey
  • Posts: 8398
  • Loc: USA

Post 3+ Months Ago

UPSGuy wrote:
I think preg_match is particular about it's parameter types, but I could be way off - just a shot in the dark. Have you tried saving that value to a var and then using that var instead of the array call?

Code: [ Select ]
 
#     $email = $regex['EMAIL'];
#     if(!preg_match($email, $web))
#     {
#         ++$error;
#         die($email);
#     }
#     $url = $regex['URL'];
#     if(!preg_match($url, $web))
#     {
#         ++$error;
#         die($web);
#     }
 
  1.  
  2. #     $email = $regex['EMAIL'];
  3. #     if(!preg_match($email, $web))
  4. #     {
  5. #         ++$error;
  6. #         die($email);
  7. #     }
  8. #     $url = $regex['URL'];
  9. #     if(!preg_match($url, $web))
  10. #     {
  11. #         ++$error;
  12. #         die($web);
  13. #     }
  14.  

:lol: you were way off... I did a really stupid mistake and used $web for the $email validation... :roll:

Thanks though :lol: ... Geeze, I hate when I make these easy mistakes and spend an hour to find out what I did... kind of frustrating.
  • UPSGuy
  • Lurker ಠ_ಠ
  • Web Master
  • User avatar
  • Posts: 2733
  • Loc: Nashville, TN

Post 3+ Months Ago

lol, glad ya found it - figured I'd be way out there.

Post Information

  • Total Posts in this topic: 5 posts
  • Users browsing this forum: No registered users and 96 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.