php中的preg系列函数

  • mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )

如果subject是数组则返回数组,否则返回一个字符串。如果发生错误,返回 NULL 。修饰符e已经废弃,请使用preg_replace_callback

$pattern,$replacement,$subject 都可以是数组也是字符串

$replacement 表示后向引用的时候用\n或者$n。在$replacement中使用反斜线,必须使用4个("\\",译注:因为这首先是php的字符串,经过转义后,是两个,再经过 正则表达式引擎后才被认为是一个原文反斜线)

一个后向引用后面跟另外一个数字需要使用 ${n}m

$limit表示每个模式在每个subject上进行替换的最大次数
$count 替换的次数

  • mixed preg_replace_callback ( mixed $pattern , callable $callback , mixed $subject [, int $limit = -1 [, int &$count ]] )

这个函数的行为除了 可以指定一个 callback 替代 replacement 进行替换 字符串的计算,其他方面等同于 preg_replace()。

<?php
// 将文本中的年份增加一年.
$text = "April fools day is 04/01/2002
";
$text.= "Last christmas was 12/24/2001
";
// 回调函数
function next_year($matches)
{
  // 通常: $matches[0]是完成的匹配
  // $matches[1]是第一个捕获子组的匹配
  // 以此类推
  return $matches[1].($matches[2]+1);
}
echo preg_replace_callback(
            "|(d{2}/d{2}/)(d{4})|",
            "next_year",
            $text);

?>
April fools day is 04/01/2003
Last christmas was 12/24/2002
  • int preg_match ( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]] )

搜索subject与pattern给定的正则表达式的一个匹配.

preg_match()返回 pattern 的匹配次数。 它的值将是0次(不匹配)或1次,因为preg_match()在第一次匹配后 将会停止搜索。preg_match_all()不同于此,它会一直搜索subject 直到到达结尾。如果发生错误preg_match()返回 FALSE

$matches
如果提供了参数matches,它将被填充为搜索结果。 $matches[0]将包含完整模式匹配到的文本, $matches[1] 将包含第一个捕获子组匹配到的文本,以此类推。

$flags
flags可以被设置为以下标记值:

PREG_OFFSET_CAPTURE
如果传递了这个标记,对于每一个出现的匹配返回时会附加字符串偏移量(相对于目标字符串的)。 注意:这会改变填充到matches参数的数组,使其每个元素成为一个由 第0个元素是匹配到的字符串,第1个元素是该匹配字符串 在目标字符串subject中的偏移量。

$offset
通常,搜索从目标字符串的开始位置开始。可选参数 offset 用于 指定从目标字符串的某个位置开始搜索(单位是字节)。

<?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
        )

)
  • int preg_match_all ( string $pattern , string $subject [, array &$matches [, int $flags = PREG_PATTERN_ORDER [, int $offset = 0 ]]] )

返回完整匹配次数(可能是0),或者如果发生错误返回FALSE。

matches
多维数组,作为输出参数输出所有匹配结果, 数组排序通过flags指定。

flags

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);
echo $out[0][0] . ", " . $out[0][1] . "
";
echo $out[1][0] . ", " . $out[1][1] . "
";
?>
<b>example: </b>, <div align=left>this is a test</div>
example: , this is a test

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);
echo $out[0][0] . ", " . $out[0][1] . "
";
echo $out[1][0] . ", " . $out[1][1] . "
";
?>
<b>example: </b>, example:
<div align="left">this is a test</div>, this is a test

PREG_OFFSET_CAPTURE

如果这个标记被传递,每个发现的匹配返回时会增加它相对目标字符串的偏移量。 注意这会改变matches中的每一个匹配结果字符串元素,使其 成为一个第0个元素为匹配结果字符串,第1个元素为 匹配结果字符串在subject中的偏移量。

  • array preg_grep ( string $pattern , array $input [, int $flags = 0 ] )

返回给定数组input中与模式pattern 匹配的元素组成的数组.

flags

如果设置为PREG_GREP_INVERT, 这个函数返回输入数组中与 给定模式pattern匹配的元素组成的数组.

$array = array(10.2,43,'ab',3.4,5.5);
$fl_array = preg_grep("/^(d+)?.d+$/", $array,1);
print_r($fl_array);
Array ( [1] => 43 [2] => ab )
  • string preg_quote ( string $str [, string $delimiter = NULL ] )

转义正则表达式字符

preg_quote()需要参数 str 并向其中 每个正则表达式语法中的字符前增加一个反斜线。 这通常用于你有一些运行时字符串 需要作为正则表达式进行匹配的时候。

正则表达式特殊字符有: . + * ? [ ^ ] $ ( ) { } = ! < > | : -

$textbody = "This book is *very* difficult to find.";
$word = "*very*";
$textbody = preg_replace ("/" . preg_quote($word) . "/",
                          "<i>" . $word . "</i>",
                          $textbody);
echo $textbody;

This book is *very* difficult to find.

另:

php中正则修饰符---http://www.cnblogs.com/simpman/p/4150416.html

与 ---http://www.cnblogs.com/simpman/p/4162311.html

原文地址:https://www.cnblogs.com/simpman/p/4166434.html