php PCRE函数的使用

替换字符串

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

两函数的区别:

<?php
$subject = array('1', 'a', '2', 'b', '3', 'A', 'B', '4'); 
$pattern = array('/\d/', '/[a-z]/', '/[1a]/'); 
$replace = array('A:$0', 'B:$0', 'C:$0'); 

echo "preg_filter returns\n";
print_r(preg_filter($pattern, $replace, $subject)); 

echo "preg_replace returns\n";
print_r(preg_replace($pattern, $replace, $subject)); 

结果:

preg_filter returns
Array
(
    [0] => A:C:1
    [1] => B:C:a
    [2] => A:2
    [3] => B:b
    [4] => A:3
    [7] => A:4
)
preg_replace returns
Array
(
    [0] => A:C:1
    [1] => B:C:a
    [2] => A:2
    [3] => B:b
    [4] => A:3
    [5] => A
    [6] => B
    [7] => A:4
)

preg_filter()等价于preg_replace() ,除了preg_filter仅仅返回经过替换的数组元素。

匹配字符串

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

preg_match()在第一次匹配后 将会停止搜索。preg_match_all()不同于此,它会一直搜索subject 直到到达结尾。 如果发生错误,都返回 FALSE

返回匹配模式的数组

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

例:

<?php

$subject = array(34,23.34,3445,54.23); 
// 返回所有包含浮点数的元素
$fl_array = preg_grep("/^(\d+)?\.\d+$/", $subject);
var_dump($fl_array);

结果:
这里写图片描述

返回使用input中key做索引的数组.

回调替换

函数: preg_replace_callback

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

例:

<?php
// 将文本中的年份增加一年.
$text = "April fools day is 04/01/2002\n";
$text.= "Last christmas was 12/24/2001\n";
// 回调函数
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

函数:preg_replace_callback_array

mixed preg_replace_callback_array ( array $patterns_and_callbacks , mixed $subject [, int $limit = -1 [, int &$count ]] )

例:

<?php
$subject = 'Aaaaaa Bbb';

preg_replace_callback_array(
    [
        '/[a]+/i' => function ($match) {
            echo strlen($match[0]), ' matches for "a" found', PHP_EOL;
        },
        '/[b]+/i' => function ($match) {
            echo strlen($match[0]), ' matches for "b" found', PHP_EOL;
        }
    ],
    $subject
);

结果

6 matches for "a" found
3 matches for "b" found

分割字符串

array preg_split ( string $pattern , string $subject [, int $limit = -1 [, int $flags = 0 ]] )

例:

<?php
//使用逗号或空格(包含" ", \r, \t, \n, \f)分隔短语
$keywords = preg_split("/[\s,]+/", "hypertext language, programming");
print_r($keywords);

结果:

Array
(
    [0] => hypertext
    [1] => language
    [2] => programming
)

猜你喜欢

转载自blog.csdn.net/change_any_time/article/details/80100851
今日推荐