Current location: Hot Scripts Forums » Programming Languages » PHP » Help with part of a PHP code


Help with part of a PHP code

Reply
  #1 (permalink)  
Old 07-27-05, 08:36 AM
cynebald cynebald is offline
Newbie Coder
 
Join Date: Jul 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Question Help with part of a PHP code

if ($action == "send")
{
if (ereg("[A-Za-z0-9_-]+([\.]{1}[A-Za-z0-9_-]+)*@[A-Za-z0-9-]+([\.]{1}[A-Za-z0-9-]+)+", $to))
{
echo "Sending $message to $to<br><br><br>";
}
else
{
die("Invalid email address ($to)");
}

Could someone explain the bolded text above? First, ereg. Second, [A-Za-z0-9_-]+([\.]{1}[A-Za-z0-9_-]+)*@[A-Za-z0-9-]+([\.]{1}[A-Za-z0-9-]+)+ between double quotations. The second is apparently an e-mail validation, but I don't understand how it works.

I'd appreciate any help.
Reply With Quote
  #2 (permalink)  
Old 07-27-05, 09:14 AM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
Quote:
Originally Posted by cynebald
if ($action == "send")
{
if (ereg("[A-Za-z0-9_-]+([\.]{1}[A-Za-z0-9_-]+)*@[A-Za-z0-9-]+([\.]{1}[A-Za-z0-9-]+)+", $to))
{
echo "Sending $message to $to<br><br><br>";
}
else
{
die("Invalid email address ($to)");
}

Could someone explain the bolded text above? First, ereg. Second, [A-Za-z0-9_-]+([\.]{1}[A-Za-z0-9_-]+)*@[A-Za-z0-9-]+([\.]{1}[A-Za-z0-9-]+)+ between double quotations. The second is apparently an e-mail validation, but I don't understand how it works.

I'd appreciate any help.
ereg matches regular expressions. "The A-Za-z0-9_-" matches upper and lowercase letters plus 0 through 9, plus a an underscore or hyphen.

the * indicates any number of chars may be matched. The second part is the same except the period (.) is backslashed to escape it. Essentially this tries to match a valid email address: some chars followed by the "@" symbol followed by more chars, followed by a period and chars for the extension. It could be made a little better (specify a max number of chars for the extension, for example) but should work fine in most instances.



Quick RegExp Guide


Modifiers
i case-insensitive pattern matching.
g global replace, or replace all
m Treat string as multiple lines. That is, change ``^'' and ``$'' from matching at only the very start or end of the string to the start or end of any line anywhere within the string
s Treat string as single line. That is, change ``.'' to match any character whatsoever, even a newline, which it normally would not match.
x Extend your pattern's legibility by permitting whitespace and comments.

Special Characters
The following should be escaped if you are trying to match that character

\ ^ . $ | ( ) [ ]
* + ? { } ,

Special Character Definitions
\ Quote the next metacharacter
^ Match the beginning of the line
. Match any character (except newline)
$ Match the end of the line (or before newline at the end)
| Alternation
() Grouping
[] Character class
* Match 0 or more times
+ Match 1 or more times
? Match 1 or 0 times
{n} Match exactly n times
{n,} Match at least n times
{n,m} Match at least n but not more than m times
More Special Character Stuff
\t tab (HT, TAB)
\n newline (LF, NL)
\r return (CR)
\f form feed (FF)
\a alarm (bell) (BEL)
\e escape (think troff) (ESC)
\033 octal char (think of a PDP-11)
\x1B hex char
\c[ control char
\l lowercase next char (think vi)
\u uppercase next char (think vi)
\L lowercase till \E (think vi)
\U uppercase till \E (think vi)
\E end case modification (think vi)
\Q quote (disable) pattern metacharacters till \E
Even More Special Characters
\w Match a "word" character (alphanumeric plus "_")
\W Match a non-word character
\s Match a whitespace character
\S Match a non-whitespace character
\d Match a digit character
\D Match a non-digit character
\b Match a word boundary
\B Match a non-(word boundary)
\A Match only at beginning of string
\Z Match only at end of string, or before newline at the end
\z Match only at end of string
\G Match only where previous m//g left off (works only with /g)
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Reply With Quote
  #3 (permalink)  
Old 07-28-05, 11:08 PM
cynebald cynebald is offline
Newbie Coder
 
Join Date: Jul 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks, End User. This helped.
Reply With Quote
  #4 (permalink)  
Old 07-29-05, 05:07 AM
Sabu Sabu is offline
Junior Code Guru
 
Join Date: Sep 2004
Posts: 458
Thanks: 0
Thanked 0 Times in 0 Posts
This is the guide I've been looking for! Finally, a simple list of what those various special characters mean. Thanks.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
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

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

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP multi-dimensional array sorting issue aqw PHP 2 06-24-05 11:09 PM
Need Help In PHP Programming for a single code suggestion !! CyberRomeo PHP 1 11-27-04 10:38 AM
Can anyone help me ? (problem using php variables in html db insert code) chronic_ PHP 2 06-13-04 11:19 AM
How to sale php code to customer without giving him code pradeep_soft PHP 4 03-12-04 12:10 PM
protecting code in PHP ckb PHP 12 01-02-04 08:53 AM


All times are GMT -5. The time now is 06:59 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.