PHP中preg_replace函数的解释

时间:2015-07-12 04:18:14

标签: php regex

preg_replace()函数有很多可能的值,例如:

    <?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');

做什么:

 \3/\4/\1\2

/(19|20)(\d{2})-(\d{1,2})-(\d{1,2})/','/^\s*{(\w+)}\s*=/ 

意思?

是否有任何信息可以帮助理解一个地方的含义?任何帮助或文件将不胜感激!在此先感谢。

2 个答案:

答案 0 :(得分:4)

查看http://www.tutorialspoint.com/php/php_regular_expression.htm

\3是被捕获的组3
\4是被捕获的组4 ......等等...

\w表示任何单词字符 \d表示任何数字 \s表示任何空白区域 +表示至少匹配前一个模式一次或多次 *表示匹配前面的模式0次或更多次 {n,m}表示至少n次匹配前一个模式至最多m{n}表示与前一个模式完全匹配n(n,}表示匹配前面的模式至少n次或更多次 (...)是一个被捕获的群体。

答案 1 :(得分:3)

所以,首先要指出的是,我们有一个模式数组$patterns)和一个替换数组({{ 1}})。让我们采取每种模式和替换,并将其分解:

模式:

$replace

的更换:

/(19|20)(\d{2})-(\d{1,2})-(\d{1,2})/

这需要一个日期并将其从\3/\4/\1\2 格式转换为YYYY-M-D格式。让我们分解它的组成部分:

M/D/YYYY

此匹配由/ ... / # The starting and trailing slash mark the beginning and end of the expression. (19|20) # Matches either 19 or 20, capturing the result as \1. # \1 will be 19 or 20. (\d{2}) # Matches any two digits (must be two digits), capturing the result as \2. # \2 will be the two digits captured here. - # Literal "-" character, not captured. (\d{2}) # Either 1 or 2 digits, capturing the result as \3. # \3 will be the one or two digits captured here. - # Literal "-" character, not captured. (\d{2}) # Either 1 or 2 digits, capturing the result as \4. # \4 will be the one or two digits captured here. 取代,表示:

\3/\4/\1\2

模式:

\3 # The two digits captured in the 3rd set of `()`s, representing the month.
/  # A literal '/'.
\4 # The two digits captured in the 4rd set of `()`s, representing the day.
/  # A literal '/'.
\1 # Either '19' or '20'; the first two digits captured (first `()`s).
\2 # The two digits captured in the 2nd set of `()`s, representing the last two digits of the year.

的更换:

/^\s*{(\w+)}\s*=/

这会将变量名称编码为$\1 = 并将其转换为{variable}。让我们分解一下:

$variable = <date>

此匹配由/ ... / # The starting and trailing slash mark the beginning and end of the expression. ^ # Matches the beginning of the string, anchoring the match. # If the following character isn't matched exactly at the beginning of the string, the expression won't match. \s* # Any whitespace character. This can include spaces, tabs, etc. # The '*' means "zero or more occurrences". # So, the whitespace is optional, but there can be any amount of it at the beginning of the line. { # A literal '{' character. (\w+) # Any 'word' character (a-z, A-Z, 0-9, _). This is captured in \1. # \1 will be the text contained between the { and }, and is the only thing "captured" in this expression. } # A literal '}' character. \s* # Any whitespace character. This can include spaces, tabs, etc. = # A literal '=' character. 取代,表示:

$\1 =

最后,我想向您展示一些资源。您正在使用的正则表达式格式称为“PCRE”或Perl兼容的正则表达式。 Here是关于PHP的PCRE的快速备忘单。在过去几年中,已经出现了一些工具来帮助您可视化,解释和测试正则表达式。一个是Regex 101(只是Google“正则表达式测试程序”或“正则表达式可视化工具”)。如果你看here,这是对第一个RegEx的解释,here是对第二个的解释。还有其他一些,例如DebuggexRegex Tester等。但我发现Regex 101上的详细匹配细分非常有用。