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

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

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

      示例1
<?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));
 ?>

阅读全文 »

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

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

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

阅读全文 »

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

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

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

阅读全文 »

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

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

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

阅读全文 »

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

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

      示例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] . "\n";
echo $out[1][0] . ", " . $out[1][1] . "\n";
?>

      示例2
<?php 
preg_match_all(    '/(?J)(?<match>foo)|(?<match>bar)/',    'foo bar',    $matches,    PREG_PATTERN_ORDER);
print_r($matches['match']);
?>

      示例3
<?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] . "\n";
echo $out[1][0] . ", " . $out[1][1] . "\n";
?>

      示例4
<?php 
preg_match_all('/(foo)(bar)(baz)/', 'foobarbaz', $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
?>

      示例5
<?php 
preg_match_all("/\(?  (\d{
3}
)?  \)?  (?(1)  [\-\s] ) \d{
3}
-\d{
4}
/x",                "Call 555-1212 or 1-800-555-1212", $phones);
?>

      示例6
<?php 
//\\2是一个后向引用的示例. 这会告诉pcre它必须匹配正则表达式中第二个圆括号(这里是([\w]+))//匹配到的结果. 这里使用两个反斜线是因为这里使用了双引号.$html = "<b>bold text</b><a href=howdy.html>click me</a>";
preg_match_all("/(<([\w]+)[^>]*>)(.*?)(<\/\\2>)/", $html, $matches, PREG_SET_ORDER);
foreach ($matches as $val) {
    echo "matched: " . $val[0] . "\n";
    echo "part 1: " . $val[1] . "\n";
    echo "part 2: " . $val[2] . "\n";
    echo "part 3: " . $val[3] . "\n";
    echo "part 4: " . $val[4] . "\n\n";
}
?>

      示例7
<?php 
$str = <<<FOOa: 1b: 2c: 3FOO;
preg_match_all('/(?P<name>\w+): (?P<digit>\d+)/', $str, $matches);
/* 选择方式 */
// preg_match_all('/(?<name>\w+): (?<digit>\d+)/', $str, $matches);
print_r($matches);
?>

阅读全文 »

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

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

      示例1
<?php 
preg_match('/(foo)(bar)(baz)/', 'foobarbaz', $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
?>

      示例2
<?php 
preg_match('/(a)(b)*(c)/', 'ac', $matches);
var_dump($matches);
preg_match('/(a)(b)*(c)/', 'ac', $matches, PREG_UNMATCHED_AS_NULL);
var_dump($matches);
?>

      示例3
<?php 
$subject = "abcdef";
$pattern = '/^def/';
preg_match($pattern, $subject, $matches, PREG_OFFSET_CAPTURE, 3);
print_r($matches);
?>

      示例4
<?php 
$subject = "abcdef";
$pattern = '/^def/';
preg_match($pattern, substr($subject,3), $matches, PREG_OFFSET_CAPTURE);
print_r($matches);
?>

      示例5
<?php 
//模式分隔符后的"i"标记这是一个大小写不敏感的搜索if (preg_match("/php/i", "PHP is the web scripting language of choice.")) {
    echo "A match was found.";
}
 else {
    echo "A match was not found.";
}
?>

      示例6
<?php 
/* 模式中的\b标记一个单词边界,所以只有独立的单词"web"会被匹配,而不会匹配 * 单词的部分内容比如"webbing" 或 "cobweb" */
if (preg_match("/\bweb\b/i", "PHP is the web scripting language of choice.")) {
    echo "A match was found.";
}
 else {
    echo "A match was not found.";
}
if (preg_match("/\bweb\b/i", "PHP is the website scripting language of choice.")) {
    echo "A match was found.";
}
 else {
    echo "A match was not found.";
}
?>

      示例7
<?php 
//从URL中获取主机名称preg_match('@^(?:http://)?([^/]+)@i',    "http://www.php.net/index.html", $matches);
$host = $matches[1];
//获取主机名称的后面两部分preg_match('/[^.]+\.[^.]+$/', $host, $matches);
echo "domain name is: {
$matches[0]}
\n";
?>

      示例8
<?php 
$str = 'foobar: 2008';
preg_match('/(?P<name>\w+): (?P<digit>\d+)/', $str, $matches);
/* 可选的方式 */
// preg_match('/(?<name>\w+): (?<digit>\d+)/', $str, $matches);
print_r($matches);
?>

阅读全文 »

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

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

      示例1
<?php 
$keywords = '$40 for a g3/400';
$keywords = preg_quote($keywords, '/');
echo $keywords;
 // 返回 \$40 for a g3\/400?>

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

阅读全文 »

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

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

      示例1
<?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);
?>

阅读全文 »

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

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

      示例1
<?php 
/* 一个unix样式的命令行过滤器,用于将段落开始部分的大写字母转换为小写。 */
$fp = fopen("php://stdin", "r") or die("can't read stdin");
while (!feof($fp)) {
    $line = fgets($fp);
    $line = preg_replace_callback(        '|<p>\s*\w|',        function ($matches) {
            return strtolower($matches[0]);
        }
,        $line    );
    echo $line;
}
fclose($fp);
?>

      示例2
<?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);
?>

      示例3
<?php 
$input = "plain [indent] deep [indent] deeper [/indent] deep [/indent] plain";
function parseTagsRecursive($input){
     /* 译注: 对此正则表达式分段分析     * 首尾两个#是正则分隔符     * \[indent] 匹配一个原文的[indent]     * ((?:[^[]|\[(?!/?indent])|(?R))+)分析:     *   (?:[^[]|\[(?!/?indent])分析:     *  首先它是一个非捕获子组     *   两个可选路径, 一个是非[字符, 另一个是[字符但后面紧跟着不是/indent或indent.     *   (?R) 正则表达式递归     *     \[/indent] 匹配结束的[/indent]     * /    $regex = '#\[indent]((?:[^[]|\[(?!/?indent])|(?R))+)\[/indent]#';
    if (is_array($input)) {
        $input = '<div style="margin-left: 10px">'.$input[1].'</div>';
    }
    return preg_replace_callback($regex, 'parseTagsRecursive', $input);
}
$output = parseTagsRecursive($input);
echo $output;
?>

阅读全文 »

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

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

      示例1
<?php 
$string = 'April 15, 2003';
$pattern = '/(\w+) (\d+), (\d+)/i';
$replacement = '${
1}
1,$3';
echo preg_replace($pattern, $replacement, $string);
?>

      示例2
<?php 
$string = 'The quick brown fox jumps over the lazy dog.';
$patterns = array();
$patterns[0] = '/quick/';
$patterns[1] = '/brown/';
$patterns[2] = '/fox/';
$replacements = array();
$replacements[2] = 'bear';
$replacements[1] = 'black';
$replacements[0] = 'slow';
echo preg_replace($patterns, $replacements, $string);
?>

      示例3
<?php 
ksort($patterns);
ksort($replacements);
echo preg_replace($patterns, $replacements, $string);
?>

      示例4
<?php 
$patterns = array ('/(19|20)(\d{
2}
)-(\d{
1,2}
)-(\d{
1,2}
)/',                   '/^\s*{
(\w+)}
\s*=/');
$replace = array ('\3/\4/\1\2', '$\1 =');
echo preg_replace($patterns, $replace, '{
startDate}
 = 1999-5-27');
?>

      示例5
<?php 
$str = 'foo   o';
$str = preg_replace('/\s\s+/', ' ', $str);
// 将会改变为'foo o'echo $str;
?>

      示例6
<?php 
$count = 0;
echo preg_replace(array('/\d/', '/\s/'), '*', 'xp 4 to', -1 , $count);
echo $count;
 //3?>

阅读全文 »

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

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

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

      示例2
<?php 
$str = 'string';
$chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
print_r($chars);
?>

      示例3
<?php 
$str = 'hypertext language programming';
$chars = preg_split('/ /', $str, -1, PREG_SPLIT_OFFSET_CAPTURE);
print_r($chars);
?>

阅读全文 »

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