部分内容来自:http://www.nowamagic.net/librarys/veda/detail/1054
preg_match — 进行正则表达式匹配。
语法:int preg_match ( string $pattern , string $subject [, array $matches [, int $flags ]] )
在 subject 字符串中搜索与 pattern 给出的正则表达式相匹配的内容。如果提供了 matches ,则其会被搜索的结果所填充。$matches[0] 将包含与整个模式匹配的文本,$matches[1] 将包含与第一个捕获的括号中的子模式所匹配的文本,以此类推。
参数 | 说明 |
---|---|
pattern | 正则表达式 |
subject | 需要匹配检索的对象 |
matches | 可选,存储匹配结果的数组, $matches[0] 将包含与整个模式匹配的文本,$matches[1] 将包含与第一个捕获的括号中的子模式所匹配的文本,以此类推 |
flags 可以是下列标记:PREG_OFFSET_CAPTURE。如果设定本标记,对每个出现的匹配结果也同时返回其附属的字符串偏移量。注意这改变了返回的数组的值,使其中的每个单元也是一个数组,其中第一项为匹配字符串,第二项为其偏移量。本标记自 PHP 4.3.0 起可用。
flags 参数自 PHP 4.3.0 起可用。
preg_match() 返回 pattern 所匹配的次数。要么是 0 次(没有匹配)或 1 次,因为 preg_match() 在第一次匹配之后将停止搜索。preg_match_all() 则相反,会一直搜索到 subject 的结尾处。如果出错 preg_match() 返回 FALSE。
如果只想查看一个字符串是否包含在另一个字符串中,不要用 preg_match()。可以用 strpos() 或 strstr() 替代,要快得多。
获取Google首页title
比如说要获取google首页的title内容,代码如下:
1 |
<?php |
2 |
$str = file_get_contents ( 'http://www.google.com' ); |
3 |
preg_match( '/<title>(.*)</title>/' , $str , $arr ); |
4 |
echo $arr [1]; |
5 |
?> |
从网址获取域名
1 |
<?php |
2 |
preg_match( "/^(http://)?([^/]+)/i" , "http://www.nowamagic.net/index.html" , $matches ); |
3 |
$host = $matches [2]; // 从主机名中取得后面两段 |
4 |
preg_match( "/[^./]+.[^./]+$/" , $host , $matches ); |
5 |
echo "domain name is: {$matches[0]}
" ; |
6 |
?> |
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:
1 |
Array ( |
2 |
[0] => http: //www.nowamagic.net |
3 |
[1] => http: // |
4 |
[2] => www.nowamagic.net ) |
$matches[0] 将包含与整个模式匹配的文本,$matches[1] 将包含与第一个捕获的括号中的子模式所匹配的文本。在正则中,()代表模式:匹配 pattern 并获取这一匹配。所获取的匹配可以从产生的 Matches 集合得到,在VBScript 中使用 SubMatches 集合,在JScript 中则使用 $0…$9 属性。就是说数组中下标为1的值就是正则中/^(http://)?([^/]+)/i第一个()里的值!数组下标2的值以此类推。
在文本中搜索特定字符串
1 |
<?php |
2 |
// 模式定界符后面的 "i" 表示不区分大小写字母的搜索 |
3 |
if (preg_match ( "/nowamagic/i" , "Welcome to nowamagic.net." )) { |
4 |
print "A match was found." ; |
5 |
} else { |
6 |
print "A match was not found." ; |
7 |
} |
8 |
?> |
楼主自己加的:
关于preg_match()这个函数,楼主之前一直比较苦恼。开始不懂正则表达式,以至于$matches数组里存储的东西一直不知道怎么来的。之后看了一些正则表达式,才了然了。然后这里我想说的是这个函数的另外两个参数,flags和offset。
最新的手册上:
int preg_match ( string $pattern
, string $subject
[, array &$matches
[, int $flags
= 0 [, int $offset
= 0 ]]] )
flags
flags
可以被设置为以下标记值:
PREG_OFFSET_CAPTURE
- 如果传递了这个标记,对于每一个出现的匹配返回时会附加字符串偏移量(相对于目标字符串的)。 注意:这会改变填充到
matches
参数的数组,使其每个元素成为一个由 第0个元素是匹配到的字符串,第1个元素是该匹配字符串 在目标字符串subject
中的偏移量。
offset
通常,搜索从目标字符串的开始未知开始。可选参数 offset
用于 指定从目标字符串的某个未知开始搜索(单位是字节)。
先说flags,其实手册上说的很清楚。需要举个例子。
1 <?php
2 $subject = "bcdef";
3 $pattern = '/def/';
4 preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE);
5
6 echo "<pre>";
7 print_r($matches);
8 echo "<pre>";
9 ?>
结果为:
Array
(
[0] => Array
(
[0] => def
[1] => 2
)
)
填充的$matches数组的$matches[0][1]被填充了偏移量,也就是2.因为是扫过bc两个字符后,才找到def。所以偏移量是2.
flags的可选项貌似只有PREG_OFFSET_CAPTURE一个,手册上并没有给出其他的。
下面说offset参数。给出该参数后呢,搜索$subject的时候从offset的指定位置开始搜索,下面给出例子。
<?php
$subject = "bcdef";
$pattern = '/def/';
preg_match($pattern, $subject, $matches1, PREG_OFFSET_CAPTURE,2);
preg_match($pattern, $subject, $matches2, PREG_OFFSET_CAPTURE,3);
echo "<pre>";print_r($matches1);echo "<pre>";
echo "<pre>";print_r($matches2);echo "<pre>";
?>
结果为:
Array
(
[0] => Array
(
[0] => def
[1] => 2
)
)
Array
(
)
$matches1数组是从$subject字符串偏移量2之后开始的搜索,所以找到了匹配项def;而$matches2数组是$subjec字符串偏移量3之后(只剩下ef)搜索,所以无法找到匹配。所以$matches2为空。
以上是楼主的一点小心得,请大家指正。也希望能帮到大家更好的理解preg_matches()这个函数。