Re: real simple regex
| From: | * R&zE: | Date: | Tue, 18 Sep 2001 07:38:58 +0000 |
| Subject: | Re: real simple regex | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-67676@lists.php.net to get a copy of this message | ||
<Original message>
From: Jack Dempsey <dempsejn@georgetown.edu>
Date: Mon, Sep 17, 2001 at 09:41:07PM -0400
Message-ID: <NEBBKBGAMLPMIDAJEALPIEIMCHAA.dempsejn@georgetown.edu>
Subject: RE: [PHP] real simple regex
> you actually don't need regex...
> if you take a few minutes you can do it all with strpos and substr, adn
> it'll be faster than regex.....
</Original message>
<Reply>
Not realy the very best solution to use strpos and substr. You'll
have to loop through the "From-string" and look for the next address
each time. You can have more than one address in the From-field! And
then again... what would you do with addresses that are not between
< and >? Eg.: Renze Munnik <renze@datalink.nl>, someone@hotmail.com.
Using strpos and substr won't realy make you're live any simpler.
I suggest you use something like the following RE:
$emlchars = "[a-z0-9.-]+";
$RE = "/($emlchars@$emlchars)/i";
I've put the allowed characters for the email-address in a separate
string in order to make it read easier. You can add all characters
to that string that are also allowed, because I don't believe these
are the only allowed characters in an email-address.
How to use? See the following example code:
--- PHP Code ---
<PRE>
<?php
$from = "* R&zE: <renze@datalink.nl>,
someone@hotmail.com,
Mr Nobody <nobody@some.domain.com>";
$emlchars = "[a-z0-9.-]+";
$RE = "/($emlchars@$emlchars)/i";
if (preg_match_all ($RE, $from, $matches)) {
print ("<H1>Matched!</H1>\n\n");
print_r ($matches);
} else {
print ("<H1>Didn't match!</H1>");
}
?>
</PRE>
--- End of PHP Code ---
</Reply>
--
* R&zE:
-- »»»»»»»»»»»»»»»»»»»»»»»»
-- Renze Munnik
-- DataLink BV
--
-- E: renze@datalink.nl
-- W: +31 23 5326162
-- F: +31 23 5322144
-- M: +31 6 21811143
--
-- Stationsplein 82
-- 2011 LM HAARLEM
-- Netherlands
--
-- http://www.datalink.nl
-- ««««««««««««««««««««««««