PHP正则相关的几个函数

1、preg_grep()

函数原型:array preg_grep (string $pattern, array $input)
PHP函数preg_grep()返回一个数组,其中包括了$input数组中与给定的$pattern模式相匹配的单元。对于输入数组$input中的每个元素,preg_grep()也只进行一次匹配。代码给出的示例简单地说明了preg_grep()函数的使用。

 

<?php
 $subjects = array(  "Mechanical Engineering", "Medicine",  "Social Science", "Agriculture",  "Commercial Science", "Politics"  );
 //匹配所有仅由有一个单词组成的科目名
 $alonewords = preg_grep("/^[a-z]*$/i", $subjects); 
 print_r($alonewords );
?>

 


输出
Array
(
    [1] => Medicine
    [3] => Agriculture
    [5] => Politics
)
 

 

2、preg_match

 

<?php
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";
?>

这是手册里的例子。
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:
Array ( [0] =>
http://www.php.net [1] => http:// [2] => www.php.net )
$matches[0] 将包含与整个模式匹配的文本,$matches[1] 将包含与第一个捕获的括号中的子模式所匹配的文本。在正则中,()代表模式:匹配 pattern 并获取这一匹配。所获取的匹配可以从产生的 Matches 集合得到,在VBScript 中使用 SubMatches 集合,在JScript 中则使用 $0…$9 属性。就是说数组中下标为1的值就是正则中/^(http:\/\/)?([^\/]+)/i第一个()里的值!数组下标2的值以此类推

 

 

3、preg_match_all -- 进行全局正则表达式匹配

 

int preg_match_all ( string pattern, string subject, array matches [, int flags] )

在 subject 中搜索所有与 pattern 给出的正则表达式匹配的内容并将结果以 flags 指定的顺序放到 matches 中。

搜索到第一个匹配项之后,接下来的搜索从上一个匹配项末尾开始。

flags 可以是下列标记的组合(注意把 PREG_PATTERN_ORDER 和 PREG_SET_ORDER 合起来用没有意义):

 

PREG_PATTERN_ORDER

 

对结果排序使 $matches[0] 为全部模式匹配的数组,$matches[1] 为第一个括号中的子模式所匹配的字符串组成的数组,以此类推。

例子:
<?php
preg_match_all ("|<[^>]+>(.*)</[^>]+>|U","<b>example: </b><div align=left>this is a test</div>",$out, PREG_PATTERN_ORDER);
print $out[0][0].", ".$out[0][1]."\n";
print $out[1][0].", ".$out[1][1]."\n";
?>

本例将输出:
<b>example: </b>, <div align=left>this is a test</div>
example: , this is a test

因此,$out[0] 包含匹配整个模式的字符串,$out[1] 包含一对 HTML 标记之间的字符串。

 

PREG_SET_ORDER

 

对结果排序使 $matches[0] 为第一组匹配项的数组,$matches[1] 为第二组匹配项的数组,以此类推。

例子:
<?php
preg_match_all ("|<[^>]+>(.*)</[^>]+>|U","<b>example: </b><div align=left>this is a test</div>",$out, PREG_SET_ORDER);
print $out[0][0].", ".$out[0][1]."\n";
print $out[1][0].", ".$out[1][1]."\n";
?>

本例将输出:
<b>example: </b>, example:
<div align=left>this is a test</div>, this is a test

本例中,$matches[0] 是第一组匹配结果,$matches[0][0] 包含匹配整个模式的文本,$matches[0][1] 包含匹配第一个子模式的文本,以此类推。同样,$matches[1] 是第二组匹配结果,等等。

 

PREG_OFFSET_CAPTURE

 

如果设定本标记,对每个出现的匹配结果也同时返回其附属的字符串偏移量。注意这改变了返回的数组的值,使其中的每个单元也是一个数组,其中第一项为匹配字符串,第二项为其在 subject 中的偏移量。本标记自 PHP 4.3.0 起可用。

如果没有给出标记,则假定为 PREG_PATTERN_ORDER。

返回整个模式匹配的次数(可能为零),如果出错返回 FALSE

 

<?php
$subject = "abcdef";
$pattern = '/^def/';
preg_match($pattern, substr($subject,3), $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
?>
输出

Array
(
    [0] => Array
        (
            [0] => def
            [1] => 0
        )

)

 

 

 

4、 preg_replace

mixed preg_replace ( mixed pattern, mixed replacement, mixed subject [, int limit])


在 subject 中搜索 pattern 模式的匹配项并替换为 replacement。如果指定了 limit,则仅替换 limit 个匹配,如果省略 limit 或者其值为 -1,则所有的匹配项都会被替换。

下例返回值为 $startDate = 6/19/1969
<?php
$patterns = array("/(19|20)\d{2})-(\d{1,2})-(\d{1,2})/", "/^\s*{(\w+)}\s*=/");
$replace = array("
\\3/\\4/\\1", "$\\1 =");
print preg_replace($patterns, $replace, "{startDate} = 1969-6-19");
?>

 


5、preg_split

preg_split -- 用正则表达式分割字符串
说明
array preg_split ( string pattern, string subject [, int limit [, int flags]])


返回一个数组,包含 subject 中沿着与 pattern 匹配的边界所分割的子串。

如果指定了 limit,则最多返回 limit 个子串,如果 limit 是 -1,则意味着没有限制,可以用来继续指定可选参数 flags。

 

flags 可以是下列标记的任意组合(用按位或运算符 | 组合):


PREG_SPLIT_NO_EMPTY
如果设定了本标记,则 preg_split() 只返回非空的成分。

 

PREG_SPLIT_DELIM_CAPTURE
如果设定了本标记,定界符模式中的括号表达式也会被捕获并返回。

 

PREG_SPLIT_OFFSET_CAPTURE
如果设定了本标记,对每个出现的匹配结果也同时返回其附属的字符串偏移量。注意这改变了返回的数组的值,使其中的每个单元也是一个数组,其中第一项为匹配字符串,第二项为其在 subject 中的偏移量。本标记自 PHP 4.3.0 起可用。


例子 1. preg_split() 例子

<?php
// split the phrase by any number of commas or space characters,
// which include " ", \r, \t, \n and \f
$keywords = preg_split ("/[\s,]+/", "hypertext language, programming");
?>

 

例子 2. 将字符串分割成字符

<?php
$str = 'string';
$chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
print_r($chars);
?>

 

 

例3

<?php
print_r(preg_split('/(a)/', 'mama', -1, PREG_SPLIT_DELIM_CAPTURE));
?>

本例将输出:
Array
(
    [0] => m
    [1] => a
    [2] => m
    [3] => a
    [4] =>
)

 

 

例子 4. 将字符串分割为匹配项及其偏移量

<?php
$str = 'hypertext language programming';
$chars = preg_split('/ /', $str, -1, PREG_SPLIT_OFFSET_CAPTURE);
print_r($chars);
?>

本例将输出:

Array
(
    [0] => Array
        (
            [0] => hypertext
            [1] => 0
        )

    [1] => Array
        (
            [0] => language
            [1] => 10
        )

    [2] => Array
        (
            [0] => programming
            [1] => 19
        )

)

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值