Got this function for ammending the query string and was wondering what the replacement part of the pre_replace meant (ie- $1$2$4).
得到了这个函数来对查询字符串进行ammending,并想知道pre_replace的替换部分是什么意思(即:$1$2$4)。
function add_querystring_var($url, $key, $value) {
$url = preg_replace('/(.*)(\?|&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $url . '&');
$url = substr($url, 0, -1);
if (strpos($url, '?') === false) {
return ($url . '?' . $key . '=' . $value);
} else {
return ($url . '&' . $key . '=' . $value);
}
}
Not too familiar with regular expression stuff. I get the various parts to preg_replace but not 100% about the use of '$1$2$4' in the replacement part.
不太熟悉正则表达式。我得到了preg_replace的各种部件,但不是100%关于在替换部件中使用“$1$2$4”。
3 个解决方案
#1
18
$1, $2... $n
in regular expression replaces are references to the matches wrapped in parenthesis. $0
would be the entire match, $1
would be the first parenthesized capture, $2
would be the second, etc.
$ 1、$ 2…正则表达式替换中的$n是对括号中包含的匹配的引用。$0将是整个匹配,$1将是第一个括号捕获,$2将是第二个,等等。
-
$1
is a reference to whatever is matched by the first(.*)
- $1是对第一个(.*)匹配的任何值的引用。
-
$2
is a reference to(\?|&)
- $2是引用(\?|&)
-
$4
is a reference to the second(.*)
- $4是对第二个(.*)的引用。
See the docs, specifically the replacement argument of the function:
参见文档,特别是函数的替换参数:
replacement may contain references of the form \n or (since PHP 4.0.4) $n, with the latter form being the preferred one. Every such reference will be replaced by the text captured by the n'th parenthesized pattern. n can be from 0 to 99, and \0 or $0 refers to the text matched by the whole pattern. Opening parentheses are counted from left to right (starting from 1) to obtain the number of the capturing subpattern. To use backslash in replacement, it must be doubled ("\\" PHP string).
替换可能包含表单\n或(因为PHP 4.0.4) $n的引用,后者是首选形式。每个这样的引用都将被第n个圆括号模式捕获的文本所取代。n可以是0到99,\0或$0表示整个模式匹配的文本。开始圆括号从左到右(从1开始)进行计数,以获得捕获子模式的数量。要使用反斜杠替换,它必须加倍(“\”PHP字符串)。
#2
5
Perl regular expression replacement uses match variables which are the parts inside of parentheses in the regular expression:
Perl正则表达式替换使用匹配变量,这些变量是正则表达式圆括号内的部分:
$1 $2 $3 $4
'/(.*)(\?|&)' . $key . '=[^&]+?(&)(.*)/i'
So, referring to $1
in the replacement string will substitute what was matched in the the first parentheses. $0
however will refer to the entire matching string.
因此,在替换字符串中引用$1将替代第一个括号中匹配的值。$0将引用整个匹配字符串。
You can even match the parenthetical subsets inside of the regular expression itself using a backslash instead of a dollar sign. For instance, if you wanted to replace doubled words "the", or "and":
您甚至可以使用反斜杠而不是美元符号来匹配正则表达式本身的括号子集合。例如,如果你想替换两个单词“the”或“and”:
preg_replace('/\b(the|and)\b\s*\1/', '$1', $sentence);
#3
1
The are place holders for each part of regex enclosed in parenthesis, they replace the part specified in regex.
这些是包含在括号中的regex的每个部分的位置占位符,它们替换regex中指定的部分。
#1
18
$1, $2... $n
in regular expression replaces are references to the matches wrapped in parenthesis. $0
would be the entire match, $1
would be the first parenthesized capture, $2
would be the second, etc.
$ 1、$ 2…正则表达式替换中的$n是对括号中包含的匹配的引用。$0将是整个匹配,$1将是第一个括号捕获,$2将是第二个,等等。
-
$1
is a reference to whatever is matched by the first(.*)
- $1是对第一个(.*)匹配的任何值的引用。
-
$2
is a reference to(\?|&)
- $2是引用(\?|&)
-
$4
is a reference to the second(.*)
- $4是对第二个(.*)的引用。
See the docs, specifically the replacement argument of the function:
参见文档,特别是函数的替换参数:
replacement may contain references of the form \n or (since PHP 4.0.4) $n, with the latter form being the preferred one. Every such reference will be replaced by the text captured by the n'th parenthesized pattern. n can be from 0 to 99, and \0 or $0 refers to the text matched by the whole pattern. Opening parentheses are counted from left to right (starting from 1) to obtain the number of the capturing subpattern. To use backslash in replacement, it must be doubled ("\\" PHP string).
替换可能包含表单\n或(因为PHP 4.0.4) $n的引用,后者是首选形式。每个这样的引用都将被第n个圆括号模式捕获的文本所取代。n可以是0到99,\0或$0表示整个模式匹配的文本。开始圆括号从左到右(从1开始)进行计数,以获得捕获子模式的数量。要使用反斜杠替换,它必须加倍(“\”PHP字符串)。
#2
5
Perl regular expression replacement uses match variables which are the parts inside of parentheses in the regular expression:
Perl正则表达式替换使用匹配变量,这些变量是正则表达式圆括号内的部分:
$1 $2 $3 $4
'/(.*)(\?|&)' . $key . '=[^&]+?(&)(.*)/i'
So, referring to $1
in the replacement string will substitute what was matched in the the first parentheses. $0
however will refer to the entire matching string.
因此,在替换字符串中引用$1将替代第一个括号中匹配的值。$0将引用整个匹配字符串。
You can even match the parenthetical subsets inside of the regular expression itself using a backslash instead of a dollar sign. For instance, if you wanted to replace doubled words "the", or "and":
您甚至可以使用反斜杠而不是美元符号来匹配正则表达式本身的括号子集合。例如,如果你想替换两个单词“the”或“and”:
preg_replace('/\b(the|and)\b\s*\1/', '$1', $sentence);
#3
1
The are place holders for each part of regex enclosed in parenthesis, they replace the part specified in regex.
这些是包含在括号中的regex的每个部分的位置占位符,它们替换regex中指定的部分。