美文网首页
PHP正则函数

PHP正则函数

作者: 爱折腾的傻小子 | 来源:发表于2020-12-11 11:38 被阅读0次

preg_match()

搜索 subject 与 pattern 给定的正则表达式的一个匹配。
  • 语法
int preg_match( string $pattern , string $subject [, array &$matches [, int $flags = 0 [, int $offset = 0 ]]])
  • 参数说明
    • $pattern:要搜索的模式,字符串形式。
    • $subject:输入字符串。
    • $matches:如果提供了参数matches,它将被填充为搜索结果。
      • $matches[0]将包含完整模式匹配到的文本。
      • $matches[1]将包含第一个捕获子组匹配到的文本,以此类推。
    • $flags:flags可以被设置为以下标记值
      • PREG_OFFSET_CAPTURE:如果传递了这个标记,对于每一个出现的匹配返回时会附加字符串偏移量(相对于目标字符串的)。
      • 注意:这会改变填充到matchers参数的数组,使其每个元素成为一个由 第0个元素是匹配到的字符串,第1个元素是该匹配字符串在目标字符串subject中的偏移量。
    • offset:搜索从目标字符串的开始位置开始。可选参数offset用于指定从目标字符串的某个未知开始搜索(单位是字节)。
  • 返回值
    • 返回 pattern 的匹配次数。它的值将是0次(不匹配)或1次,因为preg_match()在第一次匹配后将会停止搜索。preg_match_all()不同与此,它会一直搜索subject直到到达结尾。如果发生错误preg_match返回false。
$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 ]]])
  • 在第一个匹配找到后, 子序列继续从最后一次匹配位置搜索。
  • 参数说明:
    • $pattern: 要搜索的模式,字符串形式。
    • $subject: 输入字符串。
    • $matches: 多维数组,作为输出参数输出所有匹配结果, 数组排序通过flags指定。
    • $flags:可以结合下面标记使用(注意不能同时使用PREG_PATTERN_ORDER和 PREG_SET_ORDER)
      • PREG_PATTERN_ORDER: 结果排序为$matches[0]保存完整模式的所有匹配, $matches[1] 保存第一个子组的所有匹配,以此类推。
      • PREG_SET_ORDER: 结果排序为$matches[0]包含第一次匹配得到的所有匹配(包含子组), $matches[1]是包含第二次匹配到的所有匹配(包含子组)的数组,以此类推。
      • PREG_OFFSET_CAPTURE: 如果这个标记被传递,每个发现的匹配返回时会增加它相对目标字符串的偏移量。
    • offset: 通常, 查找时从目标字符串的开始位置开始。可选参数offset用于 从目标字符串中指定位置开始搜索(单位是字节)。
  • 返回值
    • 返回完整匹配次数(可能是0),或者如果发生错误返回FALSE。
// 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 ]] )
  • 参数
    • $pattern: 要搜索的模式,可以是字符串或一个字符串数组。
    • $replacement: 用于替换的字符串或字符串数组。
    • $subject: 要搜索替换的目标字符串或字符串数组。
    • $limit: 可选,对于每个模式用于每个 subject 字符串的最大可替换次数。 默认是-1(无限制)。
    • $count: 可选,为替换执行的次数。
  • 返回值
    • 如果 subject 是一个数组, preg_replace() 返回一个数组, 其他情况下返回一个字符串。
    • 如果匹配被查找到,替换后的 subject 被返回,其他情况下 返回没有改变的 subject。如果发生错误,返回 NULL。
$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 ]] )
  • preg_filter() 等价于preg_replace() ,但它仅仅返回与目标匹配的结果
  • 参数
    • $pattern:要搜索的模式。可以是一个字符串或字符串数组
    • $replacement:用于替换的字符串或字符串数组
    • $subject:要进行搜索和替换的字符串或字符串数组
    • $limit:可选,每个模式在每个subject上进行替换的最大次数。默认是 -1(无限)
    • $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 ] )
  • 参数
    • $pattern:要搜索的模式,字符串形式。
    • $input:输入的数组。
    • $flags:如果设置为 PREG_GREP_INVERT,这个函数返回输入数组中与给定模式 pattern 不匹配的元素组成的数组。
$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 ]] )
  • 参数
    • $pattern: 用于搜索的模式,字符串形式。
    • $subject: 输入字符串。
    • $limit: 可选,如果指定,将限制分隔得到的子串最多只有limit个,返回的最后一个 子串将包含所有剩余部分。limit值为-1, 0或null时都代表"不限制", 作为php的标准,你可以使用null跳过对flags的设置。
    • $flags: 可选,可以是任何下面标记的组合(以位或运算 | 组合):
      • PREG_SPLIT_NO_EMPTY: 如果这个标记被设置, preg_split() 将进返回分隔后的非空部分。
      • PREG_SPLIT_DELIM_CAPTURE: 如果这个标记设置了,用于分隔的模式中的括号表达式将被捕获并返回。
      • PREG_SPLIT_OFFSET_CAPTURE: 如果这个标记被设置, 对于每一个出现的匹配返回时将会附加字符串偏移量. 注意:这将会改变返回数组中的每一个元素, 使其每个元素成为一个由第0 个元素为分隔后的子串,第1个元素为该子串在subject 中的偏移量组成的数组。
  • 返回值
    • 返回一个使用 pattern 边界分隔 subject 后得到的子串组成的数组。
// 使用逗号或空格(包含" ", \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_quote() 需要参数 str 并向其中 每个正则表达式语法中的字符前增加一个反斜线。 这通常用于你有一些运行时字符串 需要作为正则表达式进行匹配的时候
  • 正则表达式特殊字符有: . \ + * ? [ ^ ] $ ( ) { } = ! < > | : -
  • 参数
    • $str: 输入字符串
    • $delimiter: 如果指定了可选参数 delimiter,它也会被转义。这通常用于 转义 PCRE 函数使用的分隔符。 / 是最通用的分隔符。
  • 返回值
    • 返回转义后的字符串
// 特殊字符转义
// 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.

相关文章

  • 9.3.8 字符串中正则的其它函数

    9.3.8 字符串中正则的其它函数 test.php demo.php

  • 一. PHP入门篇和PHP进阶篇

    PHP基础语法 PHP数组 PHP函数 PHP之类和对象 PHP字符串 PHP之正则表达式 PHP之cookie ...

  • PHP正则函数

    preg_match() 搜索 subject 与 pattern 给定的正则表达式的一个匹配。 语法 参数说明$...

  • 正则表达式基础篇

    正则表达式就是用某种模式去匹配一类字符串的一种公式。 PHP中的正则表达式 PHP中有两套正则函数preg和ere...

  • php学习笔记(四)

    正则表达式 PHP中使用PCRE库函数进行正则匹配,比如上例中的preg_match用于执行一个正则匹配,常用来判...

  • OC语言实现类似PHP的preg_replace_callbac

    php里面的preg_replace_callback函数可以在正则替换的时候,通过自定义的函数对匹配的结果进行自...

  • PHP常用正则表达式汇总

    PHP常用正则表达式汇总 正则表达式在 PHP 中的应用在 PHP 应用中,正则表达式主要用于: 正则匹配:根据正...

  • PHP使用正则的函数

    匹配一个符合条件后返回 匹配所有符合条件后返回 匹配符合条件的进行替代 正则匹配(将数组中符合条件的元素形成新数组...

  • PHP易混淆知识点大分享

    41.页面字符出现乱码,怎么解决? 正则表达式是什么?php中有哪些常用的跟正则相关的函数?请写出一个email的...

  • 9.1.1 选择PHP正则表达式的处理函数库

    9.1.1 选择PHP正则表达式的处理函数库 正则表达式简介:正则表达式是用于描述字符排列和匹配模式的一种语法规则...

网友评论

      本文标题:PHP正则函数

      本文链接:https://www.haomeiwen.com/subject/dhqrgktx.html