note 62104 added to function.preg-match-all

From: Date: Mon, 20 Feb 2006 08:53:03 +0000
Subject: note 62104 added to function.preg-match-all
Groups: php.notes 
Request: Send a blank email to php-notes+get-104052@lists.php.net to get a copy of this message
Here's some fleecy code to 1. validate RCF2822 conformity of address lists and 2. to extract the address specification (the part commonly known as 'email'). I wouldn't suggest using it for input form email checking, but it might be just what you want for other email applications. I know it can be optimized further, but that part I'll leave up to you nutcrackers. The total length of the resulting Regex is about 30000 bytes. That because it accepts comments. You can remove that by setting $cfws to $fws and it shrinks to about 6000 bytes. Conformity checking is absolutely and strictly referring to RFC2822. Have fun and email me if you have any enhancements! <?php function mime_extract_rfc2822_address($string) { //rfc2822 token setup $crlf = "(?:\r\n)"; $wsp = "[\t ]"; $text = "[\\x01-\\x09\\x0B\\x0C\\x0E-\\x7F]"; $quoted_pair = "(?:\\\\$text)"; $fws = "(?:(?:$wsp*$crlf)?$wsp+)"; $ctext = "[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F" . "!-'*-[\\]-\\x7F]"; $comment = "(\\((?:$fws?(?:$ctext|$quoted_pair|(?1)))*" . "$fws?\\))"; $cfws = "(?:(?:$fws?$comment)*(?:(?:$fws?$comment)|$fws))"; //$cfws = $fws; //an alternative to comments $atext = "[!#-'*+\\-\\/0-9=?A-Z\\^-~]"; $atom = "(?:$cfws?$atext+$cfws?)"; $dot_atom_text = "(?:$atext+(?:\\.$atext+)*)"; $dot_atom = "(?:$cfws?$dot_atom_text$cfws?)"; $qtext = "[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F!#-[\\]-\\x7F]"; $qcontent = "(?:$qtext|$quoted_pair)"; $quoted_string = "(?:$cfws?\"(?:$fws?$qcontent)*$fws?\"$cfws?)"; $dtext = "[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F!-Z\\^-\\x7F]"; $dcontent = "(?:$dtext|$quoted_pair)"; $domain_literal = "(?:$cfws?\\[(?:$fws?$dcontent)*$fws?]$cfws?)"; $domain = "(?:$dot_atom|$domain_literal)"; $local_part = "(?:$dot_atom|$quoted_string)"; $addr_spec = "($local_part@$domain)"; $display_name = "(?:(?:$atom|$quoted_string)+)"; $angle_addr = "(?:$cfws?<$addr_spec>$cfws?)"; $name_addr = "(?:$display_name?$angle_addr)"; $mailbox = "(?:$name_addr|$addr_spec)"; $mailbox_list = "(?:(?:(?:(?<=:)|,)$mailbox)+)"; $group = "(?:$display_name:(?:$mailbox_list|$cfws)?;$cfws?)"; $address = "(?:$mailbox|$group)"; $address_list = "(?:(?:^|,)$address)+"; //output length of string (just so you see how f**king long it is) echo(strlen($address_list) . " "); //apply expression preg_match_all("/^$address_list$/", $string, $array, PREG_SET_ORDER); return $array; }; ?> ---- Server IP: 217.13.201.10 Probable Submitter: 85.180.37.160 ---- Manual Page -- http://www.php.net/manual/en/function.preg-match-all.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+62104 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+62104&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+62104&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+62104&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+62104&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+62104&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+62104&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+62104&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+62104&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#104052) next »