TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Absolute Beginners (http://www.talkphp.com/absolute-beginners/)
-   -   Data validation syntax -- looks Greek to me (http://www.talkphp.com/absolute-beginners/2679-data-validation-syntax-looks-greek-me.html)

Dave 04-22-2008 02:45 PM

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;
   } 


delayedinsanity 04-22-2008 03:07 PM

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

Salathe 04-22-2008 03:16 PM

As delayedinsanity pointed out, Regular-Expressions.info is a nice resource. Also the PHP Manual has a helpful Regular Expression Detail section.

Dave 04-22-2008 03:27 PM

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

delayedinsanity 04-22-2008 03:51 PM

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


All times are GMT. The time now is 03:43 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0