note 37209 added to function.eregi
| From: | joseph dot ragsdale at xconduit dot com | Date: | Thu, 06 Nov 2003 19:31:56 +0000 |
| Subject: | note 37209 added to function.eregi | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-60016@lists.php.net to get a copy of this message | ||
I'm posting this as a - hopefully - helpful example to those who are learning regular
expressions. Apologies to the documentation team and moderators for posting yet another email
address validation example, but it's hard to argue against it as most everyone will see regular
expressions as being a solution to this problem.
<?php
// Example results:
$email[] = 'foo@example.com'; // matched
$email[] = 'foo.bar@example.co.uk'; // matched
$email[] = 'foo_bar@example.com'; // matched
$email[] = '_foo_bar@example.com'; // matched
$email[] = 'foo@example.example'; // matched
$email[] = '_-._@-.--'; // matched
$email[] = '@.'; // not matched
$email[] = '@.com'; // not matched
$email[] = ' @ .com'; // not matched
$email[] = '.bar@example.com'; // not matched
$email[] = 'foo.@example.com'; // not matched
$email[] = 'foo@example.x.y'; // not matched
$regex =
'^'.
'[_a-z0-9-]+'. /* One or more underscore, alphanumeric,
or hyphen charactures. */
'(\.[_a-z0-9-]+)*'. /* Followed by zero or more sets consisting
of a period and one or more underscore,
alphanumeric, or hyphen charactures. */
'@'. /* Followed by an "at" characture. */
'[a-z0-9-]+'. /* Followed by one or more alphanumeric
or hyphen charactures. */
'(\.[a-z0-9-]{2,})+'. /* Followed by one or more sets consisting
of a period and two or more alphanumeric
or hyphen charactures. */
'$';
foreach ($email as $example) {
if (eregi($regex, $example)) {
echo $example . ' matched<br>';
} else {
echo $example . ' not matched<br>';
}
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.eregi.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+37209
Delete -- http://master.php.net/manage/user-notes.php?action=delete+37209&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+37209&report=yes
Search -- http://master.php.net/manage/user-notes.php