今天看《PHP从入门到精通》的时候,遇到了一个例子:
上面的这个例子$1就是代表$string中的“粗体字”这三个字。
其实$1,$2,$3这类的术语和正则表达式中的反向引用有关,你可以用它们代表在你的正则表达式或者在要替换的字符串中的俘获组(或者是子模式,有()包裹的)。
举一个例子:
/*
* Replaces abcd123 with 123abcd, or asdf789 with 789asdf.
*
* The $1 here refers to the capture group ([a-z]+),
* and the $2 refers to the capture group ([0-9]+).
*/
preg_replace('/([a-z]+)([0-9]+)/', '$2$1', $str);