I found this rather useful for testing mutliple strings when developing a regex pattern.
<?php
/**
* Runs preg_match on an array of strings and returns a result set.
* @author wjaspers4[at]gmail[dot]com
* @param String $expr The expression to match against
* @param Array $batch The array of strings to test.
* @return Array
*/
function preg_match_batch( $expr, $batch=array() )
{
// create a placeholder for our results
$returnMe = array();
// for every string in our batch ...
foreach( $batch as $str )
{
// test it, and dump our findings into $found
preg_match($expr, $str, $found);
// append our findings to the placeholder
$returnMe[$str] = $found;
}
return $returnMe;
}
?>
preg_match
(PHP 4, PHP 5)
preg_match — 进行正则表达式匹配
说明
int preg_match
( string $pattern
, string $subject
[, array $matches
[, int $flags
]] )
在 subject 字符串中搜索与 pattern 给出的正则表达式相匹配的内容。
如果提供了 matches ,则其会被搜索的结果所填充。$matches[0] 将包含与整个模式匹配的文本,$matches[1] 将包含与第一个捕获的括号中的子模式所匹配的文本,以此类推。
flags 可以是下列标记:
- PREG_OFFSET_CAPTURE
- 如果设定本标记,对每个出现的匹配结果也同时返回其附属的字符串偏移量。注意这改变了返回的数组的值,使其中的每个单元也是一个数组,其中第一项为匹配字符串,第二项为其偏移量。本标记自 PHP 4.3.0 起可用。
preg_match() 返回 pattern 所匹配的次数。要么是 0 次(没有匹配)或 1 次,因为 preg_match() 在第一次匹配之后将停止搜索。preg_match_all() 则相反,会一直搜索到 subject 的结尾处。如果出错 preg_match() 返回 FALSE。
Example#1 在文本中搜索“php”
<?php
// 模式定界符后面的 "i" 表示不区分大小写字母的搜索
if (preg_match ("/php/i", "PHP is the web scripting language of choice.")) {
print "A match was found.";
} else {
print "A match was not found.";
}
?>
Example#2 搜索单词“web”
<?php
/* 模式中的 \b 表示单词的边界,因此只有独立的 "web" 单词会被匹配,
* 而不会匹配例如 "webbing" 或 "cobweb" 中的一部分 */
if (preg_match ("/\bweb\b/i", "PHP is the web scripting language of choice.")) {
print "A match was found.";
} else {
print "A match was not found.";
}
if (preg_match ("/\bweb\b/i", "PHP is the website scripting language of choice.")) {
print "A match was found.";
} else {
print "A match was not found.";
}
?>
Example#3 从 URL 中取出域名
<?php
// 从 URL 中取得主机名
preg_match("/^(http:\/\/)?([^\/]+)/i",
"http://www.php.net/index.html", $matches);
$host = $matches[2];
// 从主机名中取得后面两段
preg_match("/[^\.\/]+\.[^\.\/]+$/", $host, $matches);
echo "domain name is: {$matches[0]}\n";
?>
本例将输出:
domain name is: php.net
preg_match
wjaspers4[at]gmail[dot]com
28-Aug-2008 10:55
28-Aug-2008 10:55
seth36 at gmail dot com
10-Aug-2008 05:12
10-Aug-2008 05:12
For validation of email addresses, Cal Henderson's RFC 822 and RFC 2822 is_valid_email() functions rule all:
http://code.iamcal.com/php/rfc822/
Dino Korah AT webroot DOT com
09-Jul-2008 07:11
09-Jul-2008 07:11
preg_match and preg_replace_callback doesnt match up in the structure of the array that they fill-up for a match.
preg_match, as the example shows, supports named patterns, whereas preg_replace_callback doesnt seem to support it at all. It seem to ignore any named pattern matched.
Tim
08-Jul-2008 11:01
08-Jul-2008 11:01
I made a mistake in my previous post. Mail addresses may of course only be "exotic" in their local parts, not in the domain part. Therefore, an exotic mail address would be "exotic#%$mail@domain.com".
Tim
08-Jul-2008 05:51
08-Jul-2008 05:51
For those not so familiar with regex's, I post my algorithmic email validation routine. It can more easily be changed for individual needs than regex's. My function does NOT recognize exotic email addresses as allowed by RFC. (For example, info@exotic%&$#mail.com is a legal email address but not allowed by my function.)
-Tim
<?php
function email_is_valid($email) {
if (substr_count($email, '@') != 1)
return false;
if ($email{0} == '@')
return false;
if (substr_count($email, '.') < 1)
return false;
if (strpos($email, '..') !== false)
return false;
$length = strlen($email);
for ($i = 0; $i < $length; $i++) {
$c = $email{$i};
if ($c >= 'A' && $c <= 'Z')
continue;
if ($c >= 'a' && $c <= 'z')
continue;
if ($c >= '0' && $c <= '9')
continue;
if ($c == '@' || $c == '.' || $c == '_' || $c == '-')
continue;
return false;
}
$TLD = array (
'COM', 'NET',
'ORG', 'MIL',
'EDU', 'GOV',
'BIZ', 'NAME',
'MOBI', 'INFO',
'AERO', 'JOBS',
'MUSEUM'
);
$tld = strtoupper(substr($email, strrpos($email, '.') + 1));
if (strlen($tld) != 2 && !in_array($tld, $TLD))
return false;
return true;
}
?>
mailinglist dot php at hydras-world dot com
04-Jul-2008 05:30
04-Jul-2008 05:30
The regexp below thinks that the e-mail address:
'me@de.com' is invalid, which it is not.
'/^([a-z0-9])(([-a-z0-9._])*([a-z0-9]))*\@
([a-z0-9])([-a-z0-9_])+([a-z0-9])*
(\.([a-z0-9])([-a-z0-9_-])([a-z0-9])+)*$/i'
I modified it and it seems to work for me in my limited tests of it.
YMMV.
brferreira at grad dot ufsc dot br
26-Jun-2008 10:48
26-Jun-2008 10:48
Paperweight, this pattern worked fine for me (even for intranet adresses, like "john@localhost"; and also for subdomain emails, like "john@foo.bar.com"):
'/([a-z0-9])([-a-z0-9._])+([a-z0-9])\@
([a-z0-9])([-a-z0-9_])+([a-z0-9])
(\.([a-z0-9])([-a-z0-9_-])([a-z0-9])+)*/i'
but, still, this won't replace the "activation link", that is the better way to check if an e-mail is valid or not.
jonathan dot lydall at gmail dot removethispart dot com
27-May-2008 03:50
27-May-2008 03:50
Because making a truly correct email validation function is harder than one may think, consider using this one which comes with PHP through the filter_var function (http://www.php.net/manual/en/function.filter-var.php):
<?php
$email = "someone@domain .local";
if(!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "E-mail is not valid";
} else {
echo "E-mail is valid";
}
?>
Georg
04-Apr-2008 05:36
04-Apr-2008 05:36
In addition to reiner-keller's comment about Umlaute using setlocale (LC_ALL, 'de_DE');
To enable 'de_DE' on my Debian 4 machine I first had to:
- uncomment 'de_DE' in file /etc/locale.gen and afterwards
- run locale-gen from the shell
