PCRE 函数 业 ,精于勤 荒于嬉.

PCRE 函数 preg_filter 执行一个正则表达式搜索和替换

发表日期:2021-07-01 08:56:55 | 来源: | 分类:PCRE 函数

      示例1
1<?php
2$subject array('1''a''2''b''3''A''B''4');
3 $pattern array('/\d/''/[a-z]/''/[1a]/');
4 $replace array('A:$0''B:$0''C:$0');
5 echo "preg_filter returns\n";
6print_r(preg_filter($pattern$replace$subject));
7 echo "preg_replace returns\n";
8print_r(preg_replace($pattern$replace$subject));
9 ?>

阅读全文 »

PCRE 函数 preg_grep 返回匹配模式的数组条目

发表日期:2021-07-01 08:56:56 | 来源: | 分类:PCRE 函数

      示例1
1<?php
2// 返回所有包含浮点数的元素$fl_array = preg_grep("/^(\d+)?\.\d+$/", $array);
3?>

阅读全文 »

PCRE 函数 preg_last_error_msg Returns the error message of the last PCRE regex execution

发表日期:2021-07-01 08:56:56 | 来源: | 分类:PCRE 函数

      示例1
1<?php
2preg_match('/(?:\D+|<\d+>)*[!?]/''foobar foobar foobar');
3if (preg_last_error() !== PREG_NO_ERROR) {
4    echo preg_last_error_msg();
5}
6?>

阅读全文 »

PCRE 函数 preg_last_error 返回最后一个PCRE正则执行产生的错误代码

发表日期:2021-07-01 08:56:55 | 来源: | 分类:PCRE 函数

      示例1
1<?php
2preg_match('/(?:\D+|<\d+>)*[!?]/''foobar foobar foobar');
3if (preg_last_error() == PREG_BACKTRACK_LIMIT_ERROR) {
4    print 'Backtrack limit was exhausted!';
5}
6?>

阅读全文 »

PCRE 函数 preg_match_all 执行一个全局正则表达式匹配

发表日期:2021-07-01 08:56:56 | 来源: | 分类:PCRE 函数

      示例1
1<?php
2preg_match_all("|<[^>]+>(.*)</[^>]+>|U",    "<b>example: </b><div align=left>this is a test</div>",    $out, PREG_PATTERN_ORDER);
3echo $out[0][0] . ", " $out[0][1] . "\n";
4echo $out[1][0] . ", " $out[1][1] . "\n";
5?>
      示例2
1<?php
2preg_match_all(    '/(?J)(?<match>foo)|(?<match>bar)/',    'foo bar',    $matches,    PREG_PATTERN_ORDER);
3print_r($matches['match']);
4?>
      示例3
1<?php
2preg_match_all("|<[^>]+>(.*)</[^>]+>|U",    "<b>example: </b><div align=\"left\">this is a test</div>",    $out, PREG_SET_ORDER);
3echo $out[0][0] . ", " $out[0][1] . "\n";
4echo $out[1][0] . ", " $out[1][1] . "\n";
5?>
      示例4
1<?php
2preg_match_all('/(foo)(bar)(baz)/''foobarbaz'$matches, PREG_OFFSET_CAPTURE);
3print_r($matches);
4?>
      示例5
1<?php
2preg_match_all("/\(?  (\d{
33}
4)?  \)?  (?(1)  [\-\s] ) \d{
53}
6-\d{
74}
8/x",                "Call 555-1212 or 1-800-555-1212", $phones);
9?>
      示例6
01<?php
02//\\2是一个后向引用的示例. 这会告诉pcre它必须匹配正则表达式中第二个圆括号(这里是([\w]+))//匹配到的结果. 这里使用两个反斜线是因为这里使用了双引号.$html = "<b>bold text</b><a href=howdy.html>click me</a>";
03preg_match_all("/(<([\w]+)[^>]*>)(.*?)(<\/\\2>)/"$html$matches, PREG_SET_ORDER);
04foreach ($matches as $val) {
05    echo "matched: " $val[0] . "\n";
06    echo "part 1: " $val[1] . "\n";
07    echo "part 2: " $val[2] . "\n";
08    echo "part 3: " $val[3] . "\n";
09    echo "part 4: " $val[4] . "\n\n";
10}
11?>
      示例7
1<?php
2$str = <<<FOOa: 1b: 2c: 3FOO;
3preg_match_all('/(?P<name>\w+): (?P<digit>\d+)/'$str$matches);
4/* 选择方式 */
5// preg_match_all('/(?<name>\w+): (?<digit>\d+)/', $str, $matches);
6print_r($matches);
7?>

阅读全文 »

PCRE 函数 preg_match 执行匹配正则表达式

发表日期:2021-07-01 08:56:55 | 来源: | 分类:PCRE 函数

      示例1
1<?php
2preg_match('/(foo)(bar)(baz)/''foobarbaz'$matches, PREG_OFFSET_CAPTURE);
3print_r($matches);
4?>
      示例2
1<?php
2preg_match('/(a)(b)*(c)/''ac'$matches);
3var_dump($matches);
4preg_match('/(a)(b)*(c)/''ac'$matches, PREG_UNMATCHED_AS_NULL);
5var_dump($matches);
6?>
      示例3
1<?php
2$subject "abcdef";
3$pattern '/^def/';
4preg_match($pattern$subject$matches, PREG_OFFSET_CAPTURE, 3);
5print_r($matches);
6?>
      示例4
1<?php
2$subject "abcdef";
3$pattern '/^def/';
4preg_match($patternsubstr($subject,3), $matches, PREG_OFFSET_CAPTURE);
5print_r($matches);
6?>
      示例5
1<?php
2//模式分隔符后的"i"标记这是一个大小写不敏感的搜索if (preg_match("/php/i", "PHP is the web scripting language of choice.")) {
3    echo "A match was found.";
4}
5 else {
6    echo "A match was not found.";
7}
8?>
      示例6
01<?php
02/* 模式中的\b标记一个单词边界,所以只有独立的单词"web"会被匹配,而不会匹配 * 单词的部分内容比如"webbing" 或 "cobweb" */
03if (preg_match("/\bweb\b/i""PHP is the web scripting language of choice.")) {
04    echo "A match was found.";
05}
06 else {
07    echo "A match was not found.";
08}
09if (preg_match("/\bweb\b/i""PHP is the website scripting language of choice.")) {
10    echo "A match was found.";
11}
12 else {
13    echo "A match was not found.";
14}
15?>
      示例7
1<?php
2//从URL中获取主机名称preg_match('@^(?:http://)?([^/]+)@i',    "http://www.php.net/index.html", $matches);
3$host $matches[1];
4//获取主机名称的后面两部分preg_match('/[^.]+\.[^.]+$/', $host, $matches);
5echo "domain name is: {
6$matches[0]}
7\n";
8?>
      示例8
1<?php
2$str 'foobar: 2008';
3preg_match('/(?P<name>\w+): (?P<digit>\d+)/'$str$matches);
4/* 可选的方式 */
5// preg_match('/(?<name>\w+): (?<digit>\d+)/', $str, $matches);
6print_r($matches);
7?>

阅读全文 »

PCRE 函数 preg_quote 转义正则表达式字符

发表日期:2021-07-01 08:56:55 | 来源: | 分类:PCRE 函数

      示例1
1<?php
2$keywords '$40 for a g3/400';
3$keywords = preg_quote($keywords'/');
4echo $keywords;
5 // 返回 \$40 for a g3\/400?>
      示例2
1<?php
2//在这个例子中,preg_quote($word) 用于保持星号原文涵义,使其不使用正则表达式中的特殊语义。$textbody = "This book is *very* difficult to find.";
3$word "*very*";
4$textbody = preg_replace ("/" . preg_quote($word'/') . "/",                          "<i>" $word "</i>",                          $textbody);
5?>

阅读全文 »

PCRE 函数 preg_replace_callback_array Perform a regular expression search and replace using callbacks

发表日期:2021-07-01 08:56:56 | 来源: | 分类:PCRE 函数

      示例1
01<?php
02$subject 'Aaaaaa Bbb';
03preg_replace_callback_array(    [        '~[a]+~i' => function ($match) {
04            echo strlen($match[0]), ' matches for "a" found', PHP_EOL;
05        }
06,        '~[b]+~i' => function ($match) {
07            echo strlen($match[0]), ' matches for "b" found', PHP_EOL;
08        }
09    ],    $subject);
10?>

阅读全文 »

PCRE 函数 preg_replace_callback 执行一个正则表达式搜索并且使用一个回调进行替换

发表日期:2021-07-01 08:56:56 | 来源: | 分类:PCRE 函数

      示例1
01<?php
02/* 一个unix样式的命令行过滤器,用于将段落开始部分的大写字母转换为小写。 */
03$fp fopen("php://stdin""r"or die("can't read stdin");
04while (!feof($fp)) {
05    $line fgets($fp);
06    $line = preg_replace_callback(        '|<p>\s*\w|',        function ($matches) {
07            return strtolower($matches[0]);
08        }
09,        $line    );
10    echo $line;
11}
12fclose($fp);
13?>
      示例2
01<?php
02// 将文本中的年份增加一年.$text = "April fools day is 04/01/2002\n";
03$text.= "Last christmas was 12/24/2001\n";
04// 回调函数function next_year($matches){
05  // 通常: $matches[0]是完成的匹配  // $matches[1]是第一个捕获子组的匹配  // 以此类推  return $matches[1].($matches[2]+1);
06}
07echo preg_replace_callback(            "|(\d{
082}
09/\d{
102}
11/)(\d{
124}
13)|",            "next_year",            $text);
14?>
      示例3
01<?php
02$input "plain [indent] deep [indent] deeper [/indent] deep [/indent] plain";
03function parseTagsRecursive($input){
04     /* 译注: 对此正则表达式分段分析     * 首尾两个#是正则分隔符     * \[indent] 匹配一个原文的[indent]     * ((?:[^[]|\[(?!/?indent])|(?R))+)分析:     *   (?:[^[]|\[(?!/?indent])分析:     *  首先它是一个非捕获子组     *   两个可选路径, 一个是非[字符, 另一个是[字符但后面紧跟着不是/indent或indent.     *   (?R) 正则表达式递归     *     \[/indent] 匹配结束的[/indent]     * /    $regex '#\[indent]((?:[^[]|\[(?!/?indent])|(?R))+)\[/indent]#';
05    if (is_array($input)) {
06        $input '<div style="margin-left: 10px">'.$input[1].'</div>';
07    }
08    return preg_replace_callback($regex'parseTagsRecursive'$input);
09}
10$output = parseTagsRecursive($input);
11echo $output;
12?>

阅读全文 »

PCRE 函数 preg_replace 执行一个正则表达式的搜索和替换

发表日期:2021-07-01 08:56:56 | 来源: | 分类:PCRE 函数

      示例1
1<?php
2$string 'April 15, 2003';
3$pattern '/(\w+) (\d+), (\d+)/i';
4$replacement = '${
51}
61,$3';
7echo preg_replace($pattern$replacement$string);
8?>
      示例2
01<?php
02$string 'The quick brown fox jumps over the lazy dog.';
03$patterns array();
04$patterns[0] = '/quick/';
05$patterns[1] = '/brown/';
06$patterns[2] = '/fox/';
07$replacements array();
08$replacements[2] = 'bear';
09$replacements[1] = 'black';
10$replacements[0] = 'slow';
11echo preg_replace($patterns$replacements$string);
12?>
      示例3
1<?php
2ksort($patterns);
3ksort($replacements);
4echo preg_replace($patterns$replacements$string);
5?>
      示例4
01<?php
02$patterns array ('/(19|20)(\d{
032}
04)-(\d{
051,2}
06)-(\d{
071,2}
08)/',                   '/^\s*{
09(\w+)}
10\s*=/');
11$replace array ('\3/\4/\1\2''$\1 =');
12echo preg_replace($patterns$replace, '{
13startDate}
14 = 1999-5-27');
15?>
      示例5
1<?php
2$str 'foo   o';
3$str = preg_replace('/\s\s+/'' '$str);
4// 将会改变为'foo o'echo $str;
5?>
      示例6
1<?php
2$count = 0;
3echo preg_replace(array('/\d/''/\s/'), '*''xp 4 to', -1 , $count);
4echo $count;
5 //3?>

阅读全文 »

PCRE 函数 preg_split 通过一个正则表达式分隔字符串

发表日期:2021-07-01 08:56:56 | 来源: | 分类:PCRE 函数

      示例1
1<?php
2//使用逗号或空格(包含" ", \r, \t, \n, \f)分隔短语$keywords = preg_split("/[\s,]+/", "hypertext language, programming");
3print_r($keywords);
4?>
      示例2
1<?php
2$str 'string';
3$chars = preg_split('//'$str, -1, PREG_SPLIT_NO_EMPTY);
4print_r($chars);
5?>
      示例3
1<?php
2$str 'hypertext language programming';
3$chars = preg_split('/ /'$str, -1, PREG_SPLIT_OFFSET_CAPTURE);
4print_r($chars);
5?>

阅读全文 »

全部博文(1589)
集速网 copyRight © 2015-2022 宁ICP备15000399号-1 宁公网安备 64010402001209号
与其临渊羡鱼,不如退而结网
欢迎转载、分享、引用、推荐、收藏。