TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 04-22-2008, 02:45 PM   #1 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Default Data validation syntax -- looks Greek to me

Hi, all...

This is part of an example from one of the books I am using to learn PHP.

I understand the purpose, but I need help understanding the specifics of the

"^([_a-z.... etc.

section of the code. I've already gone to the PHP Manual and did not manage to find specific help on this.

Thanks very much...
Dave
===============================

PHP Code:
   function validate_email($email)
   {  
      
# Create the syntactical validation regular expression

      
$regexp "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)
                 (\.[a-z0-9-]+)*(\.[a-z]{2,6})$" 
;

      
# Validate the syntax

      
if (eregi($regexp$email)) return 1;
      else return 
0;
   } 
Dave is offline  
Reply With Quote
Old 04-22-2008, 03:07 PM   #2 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

It's a regular expression, and they're greek to even seasoned programmers. You can read more about them here, or check out this cheat sheet to try and learn a little about them.

Basically what a regular expression does is match patterns. Like looking for a string within a string, or a specific order of characters, numbers and symbols within a string. The one you have there is looking for an email address,

Code:
      $regexp = "^([_a-z0-9-]+)(\.[_a-z0-9-]+)*@([a-z0-9-]+)
                  (\.[a-z0-9-]+)*(\.[a-z]{2,6})$"; 

      $inenglish = "^(find anything between a-z, 0-9, or with - or _ in it, at least one or more times)(same as before, but this time it can have a period in it, so it'll match emails like email@mail.com or em.ail@mail.com)*@(that @ sign is taken literally, so it's looking for the @ sign in the middle of the email.. this pattern and the next are just like the first, looking for any combination of a-z, 0-9 with dashes, underscores or periods allowed)(see previous. The star after this means it can match the whole thing as many times as possible)*(this last pattern is only looking for a pattern which includes a-z, and it needs to be between 2 and 6 characters in length, ie, .tv, .com, .shop, etc.)$";
The ^ means it will begin at the start of the string to find the pattern it wants. Anything between ( and ) or [ and ] is a grouping of sort, and then there are modifiers which tell it how often to try and find specific characters or numbers, such as * for 0 or more times, + for one or more times.. I won't go too much more into specifics of breaking it down for now, but check out those two sites, and the cheat sheet in particular after to see if you can break it down yourself. Email regex's, though numerous in their differences, are a good starting point because they're pretty basic patterns to learn the syntax from.
-m
delayedinsanity is offline  
Reply With Quote
The Following 2 Users Say Thank You to delayedinsanity For This Useful Post:
Dave (04-22-2008), Durux (04-23-2008)
Old 04-22-2008, 03:16 PM   #3 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

As delayedinsanity pointed out, Regular-Expressions.info is a nice resource. Also the PHP Manual has a helpful Regular Expression Detail section.
Salathe is offline  
Reply With Quote
The Following User Says Thank You to Salathe For This Useful Post:
Dave (04-22-2008)
Old 04-22-2008, 03:27 PM   #4 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Default

Thank you. The repsonses were very helpful. The snippet I cited was from a larger example (in one of my books) that was meant to illustrate the passing of data to a function. But I'm going through each example and trying to learn as much as I can about each detail. Very slow going, but I suppose it is bound to start coming together at some point.

Dave
Dave is offline  
Reply With Quote
Old 04-22-2008, 03:51 PM   #5 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

I just ordered a new book myself, "PHP Objects, Patterns, and Practice". Gotta love Amazon, it's supposed to be a $50 book, and I got a used copy for 20 bones. I don't care if my books are new, they're gonna get sat on anyways.

Good luck with it, I'm sure it'll come together. PHP's a great language.
-m
delayedinsanity is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 01:29 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design