PHP正则函数

2020-12-11  本文已影响0人  爱折腾的傻小子

preg_match()

搜索 subject 与 pattern 给定的正则表达式的一个匹配。
int preg_match( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]])
$result = preg_match('#[0-9]#', 'b5fg4sgf3sd2f1g', $arr);
dump($result, $arr);
/*
1
array:1 [
  0 => "5"
]
*/
// (?:) 非捕获 参考《正则语法》文章
$result = preg_match('@^(?:http://)?([^/]+)@i', 'http://www.codercto.com/index.html', $matches);
dump($result, $matches);
/*
1
array:2 [
  0 => "http://www.codercto.com"
  1 => "www.codercto.com"
]
*/
// $flags 参数
$result = preg_match('@industr(y|ies)@', 'British industry', $matches,PREG_OFFSET_CAPTURE);
dump($result, $matches);
/*
1
array:2 [
  0 => array:2 [
    0 => "industry"
    1  => 8
  ]
  1 => array:2 [
    0 => "y"
    1 => 15
  ]
]
*/

preg_match_all()

搜索 subject 中所有匹配 pattern 给定正则表达式的匹配结果并且将它们以 flag 指定顺序输出到 matches 中。
int preg_match_all(string $pattern, string $subject [, array &$matches [, int $flags = PREG_PATTERN_ORDER [, int $offset = 0 ]]])
// PREG_PATTERN_ORDER:默认标记
$result = preg_match_all('@<b>(.*)<\/b>@U', 'Name: <b>PHP</b> <br> Title: <b>Programming Language</b>', $matches);
dump($result, $matches);
/*
2
array:2 [
  0 => array:2 [
    0 => "<b>PHP</b>"
    1 => "<b>Programming Language</b>"
  ]
  1 => array:2 [
    0 => "PHP"
    1 => "Programming Language"
  ]
]
*/

// 参数 PREG_SET_ORDER
$result = preg_match_all('@<b>(.*)<\/b>@U', 'Name: <b>PHP</b> <br> Title: <b>Programming Language</b>', $matches, PREG_SET_ORDER);
dump($result, $matches);
/*
2
array:2 [
  0 => array:2 [
    0 => "<b>PHP</b>"
    1 => "PHP"
  ]
  1 => array:2 [
    0 => "<b>Programming Language</b>"
    1 => "Programming Language"
  ]
]
*/

// 参数 PREG_OFFSET_CAPTURE
$result = preg_match_all('@<b>(.*)<\/b>@U', 'Name: <b>PHP</b> <br> Title: <b>Programming Language</b>', $matches, PREG_OFFSET_CAPTURE);
dump($result, $matches);
/*
2
array:2 [
  0 => array:2 [
    0 => array:2 [
      0 => "<b>PHP</b>"
      1 => 6
    ]
    1 => array:2 [
      0 => "<b>Programming Language</b>"
      1 => 29
    ]
  ]
  1 => array:2 [
    0 => array:2 [
      0 => "PHP"
      1 => 9
    ]
    1 => array:2 [
      0 => "Programming Language"
      1 => 32
    ]
  ]
]
*/
// \\2是一个后向引用的示例. 这会告诉pcre它必须匹配正则表达式中第二个圆括号(这里是([\w]+))
// 匹配到的结果. 这里使用两个反斜线是因为这里使用了双引号.
$result = preg_match_all('@(<([\w]+)[^>]*>)(.*?)(<\/\\2>)@', '<b>bold text</b><a href=howdy.html>click me</a>', $matches);
dump($result, $matches);
/*
2
array:5 [
  0 => array:2 [
    0 => "<b>bold text</b>"
    1 => "<a href=howdy.html>click me</a>"
  ]
  1 => array:2 [
    0 => "<b>"
    1 => "<a href=howdy.html>"
  ]
  2 => array:2 [
    0 => "b"
    1 => "a"
  ]
  3 => array:2 [
    0 => "bold text"
    1 => "click me"
  ]
  4 => array:2 [
    0 => "</b>"
    1 => "</a>"
  ]
]
*/

preg_replace()

搜索 subject 中匹配 pattern 的部分, 以 replacement 进行替换
mixed preg_replace ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )
$result = preg_replace('/(\w+) (\d+), (\d+)/i', 'codercto ${2},$3', 'google 123, 456');
dd($result);
/*
"codercto 123,456"
*/

$result = preg_replace('/\s+/', '', 'runo o   b');
dd($result);
/*
"runoob"
*/
$result = preg_replace([
        '/quick/',
        '/brown/',
        '/fox/'
    ], [
        'slow',
        'black',
        'bear'
    ], 'The quick brown fox jumped over the lazy dog.');
dd($result);    // The slow black bear jumped over the lazy dog.
$count = 0;
$result = preg_replace([
    '/\d/', '/\s/'
], '*', 'xp 4 to', -1, $count);
dd($result, $count);    // xp***to  3

preg_filter()

执行一个正则表达式搜索和替换
mixed preg_filter ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )
$count = $count1 = 0;
$result = preg_filter(
    ['/\d/', '/[a-z]/', '/[1a]/'],
    ['A:$0', 'B:$0', 'C:$0'],
    ['1', 'a', '2', 'b', '3', 'A', 'B', '4'],
    -1,
    $count
);
dump($result, $count);    
/*
array:6 [
  0 => "A:C:1"
  1 => "B:C:a"
  2 => "A:2"
  3 => "B:b"
  4 => "A:3"
  7 => "A:4"
]

8
*/

$result1 = preg_replace(
    ['/\d/', '/[a-z]/', '/[1a]/'],
    ['A:$0', 'B:$0', 'C:$0'],
    ['1', 'a', '2', 'b', '3', 'A', 'B', '4'],
    -1,
    $count1
);
dd($result1, count1);
/*
array:8 [
  0 => "A:C:1"
  1 => "B:C:a"
  2 => "A:2"
  3 => "B:b"
  4 => "A:3"
  5 => "A"
  6 => "B"
  7 => "A:4"
]

8
*/

preg_grep()

给定数组 input 中与模式 pattern 匹配的元素组成的数组
array preg_grep ( string $pattern , array $input [, int $flags = 0 ] )
$result = preg_grep(
    "/^(\d+)?\.\d+$/",
    array(1, 2, 3.4, 53, 7.9)
);
dump($result);
/*
array:2 [
  2 => 3.4
  4 => 7.9
]
*/

$result1 = preg_grep(
    "/^(\d+)?\.\d+$/",
    array(1, 2, 3.4, 53, 7.9),
    PREG_GREP_INVERT
);
dd($result1);
/*
array:3 [
  0 => 1
  1 => 2
  3 => 53
]
*/

preg_split()

通过一个正则表达式分隔字符串
array preg_split ( string $pattern , string $subject [, int $limit = -1 [, int $flags = 0 ]] )
// 使用逗号或空格(包含" ", \r, \t, \n, \f)分隔短语
$result = preg_split(
    '/[\s,]+/',
    'hypertext language, programming'
);
dump($result);
/*
array:3 [
  0 => "hypertext"
  1 => "language"
  2 => "programming"
]
*/
// 将一个字符串分隔为组成它的字符
// PREG_SPLIT_NO_EMPTY 返回结果的非空部分
$result = preg_split(
    '//',
    'helium',
    -1,
    PREG_SPLIT_NO_EMPTY
 );
 dump($result);
/*
array:6 [
  0 => "h"
  1 => "e"
  2 => "l"
  3 => "i"
  4 => "u"
  5 => "m"
]
*/
// 分隔一个字符串并获取每部分的偏移量
// PREG_SPLIT_OFFSET_CAPTURE 返回结果的非空部分
$result = preg_split(
    '/ /',
    'hypertext language programming',
    -1,
    PREG_SPLIT_OFFSET_CAPTURE
);
dump($result);
/*
array:3 [
  0 => array:2 [
    0 => "hypertext"
    1 => 0
  ]
  1 => array:2 [
    0 => "language"
    1 => 10
  ]
  2 => array:2 [
    0 => "programming"
    1 => 19
  ]
]
*/

preg_quote()

用于转义正则表达式字符
// 特殊字符转义
// PREG_SPLIT_OFFSET_CAPTURE 返回结果的非空部分
$result = preg_quote(
    '$40 for a g3/400 + - (',
    '/'
);
dump($result);  // \$40 for a g3\/400 \+ \- \(

// preg_quote($word) 用于保持星号原文涵义,使其不使用正则表达式中的特殊语义
$word = "*very*";
$result = preg_replace ("/" . preg_quote($word) . "/",
    "<i>{$word}</i>",
    'This book is *very* difficult to find.');
dump($result);  // This book is <i>*very*</i> difficult to find.
上一篇下一篇

猜你喜欢

热点阅读