php正则表达式之preg_match()用法
<?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.";
}
?>
取得当前时间
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy5671')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy5671><?php
//需要匹配的字符串。date函数返回当前时间。 "现在时刻:2012-04-20 07:31 am"
$content = "现在时刻:".date("Y-m-d h:i a");
//匹配日期和时间.
if (preg_match ("/d{4}-d{2}-d{2} d{2}:d{2} [ap]m/", $content, $m))
{
echo "匹配的时间是:" .$m[0]. "n"; //"2012-04-20 07:31 am"
}
//分别取得日期和时间
if (preg_match ("/([d-]{10}) ([d:]{5} [ap]m)/", $content, $m))
{
echo "当前日期是:" .$m[1]. "n"; //"2012-04-20"
echo "当前时间是:" .$m[2]. "n"; //"07:31 am"
}
?>
这个例子将验证出此 Email 地址为正确格式。现在让我们来看看这段正则表达式所代表的各种规则。
获取Google首页title
比如说要获取google首页的title内容,代码如下:
<?php
$str = file_get_contents('http://www.google.com');
preg_match('/<title>(.*)</title>/', $str, $arr);
echo $arr[1];
?>
从网址获取域名
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy7958')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy7958><?phppreg_match("/^(http://)?([^/] )/i", "http://www.jiaochengji.com/index.html", $matches);
$host = $matches[2]; // 从主机名中取得后面两段
preg_match("/[^./] .[^./] $/", $host, $matches);
echo "domain name is: {$matches[0]}n";
?>
preg_match($pattern,$string,$matcher)其中$pattern对应的就是/^(http://)?([^/] )/i,$string 是http://www.php.net/index.html,$match是匹配到的结果。
如果提供了 matches,则其会被搜索的结果所填充。$matches[0] 将包含与整个模式匹配的文本,$matches[1] 将包含与第一个捕获的括号中的子模式所匹配的文本,以此类推。
$matches[0] 将包含与整个模式匹配的文本。咱们用pring_r打印出来第一个$matches:
<table width="620" align="center" border="0" cellpadding="1" cellspacing="1" style="background:#FB7"> <tr> <td width="464" height="27" bgcolor="#FFE7CE"> 代码如下</td> <td width="109" align="center" bgcolor="#FFE7CE" style="cursor:pointer;" onclick="doCopy('copy9810')">复制代码</td> </tr> <tr> <td height="auto" colspan="2" valign="top" bgcolor="#FFFFFF" style="padding:10px;" class="copyclass" id=copy9810>Array (
[0] => http://www.jiaochengji.com
[1] => http://
[2] => www.jiaochengji.com )
$matches[0] 将包含与整个模式匹配的文本,$matches[1] 将包含与第一个捕获的括号中的子模式所匹配的文本。在正则中,()代表模式:匹配 pattern 并获取这一匹配。所获取的匹配可以从产生的 Matches 集合得到,在VBScript 中使用 SubMatches 集合,在JScript 中则使用 $0…$9 属性。就是说数组中下标为1的值就是正则中/^(http://)?([^/] )/i第一个()里的值!数组下标2的值以此类推。
您可能感兴趣的文章:
常用的正则表达试
解决php正则表达式验证中文的问题
通过实例学习php正则表达式之正则处理函数(preg_match,preg_match_all,preg_replace,preg_split)
php正则表达式验证手机电话
正则表达式处理函数 preg_match,preg_match_all
php邮箱检测的正则表达式一例
正则表达式中模式修正符作用详解
php正则表达匹配中文若干问题的解决方法
PHP正则提取或替换img标记属性的实例代码
php基础知识考察点之正则表达式