$subject = '0300020F005176C5A11A730004F955A2A20CD8-367C-EC5B-9D8C-2CAA0B8FE45D';echo preg_replace('/^(\w{8})\w{2}/i','$1'.'08',$subject);// 输出结果:85176C5A11A730004F955A2A20CD8-367C-EC5B-9D8C-2CAA0B8FE45D// 期望结果:0300020F085176C5A11A730004F955A2A20CD8-367C-EC5B-9D8C-2CAA0B8FE45D// 但是:preg_replace('regex','$1'.'__',$subject); 这样是没有问题的
Reply content:
$subject = '0300020F005176C5A11A730004F955A2A20CD8-367C-EC5B-9D8C-2CAA0B8FE45D';echo preg_replace('/^(\w{8})\w{2}/i','$1'.'08',$subject);// 输出结果:85176C5A11A730004F955A2A20CD8-367C-EC5B-9D8C-2CAA0B8FE45D// 期望结果:0300020F085176C5A11A730004F955A2A20CD8-367C-EC5B-9D8C-2CAA0B8FE45D// 但是:preg_replace('regex','$1'.'__',$subject); 这样是没有问题的
Quote from PHP manual preg_replace function to a back-reference description: Preg_replace
When working in replacement mode and the back reference is followed by a second number (for example, adding a text number immediately after a matching pattern), you cannot use syntax such as \1 to describe the back reference. For example, \11 will make preg_replace () do not understand what you want is a \1 back to the reference followed by a text 1, or a \11 after the reference is not followed by anything. In this case the solution is to use \${1}1. This creates a separate, forward-referencing, 1-independent source.
Your replacement part is "$108", so you need to replace it with ${1} to avoid confusion.
echo preg_replace_callback('/^(\w{8})(\w{2})/i',create_function( // single quotes are essential here, // or alternative escape all $ as \$ '$matches', 'return $matches[1].\'08\';' ),$subject);
这样,callback处理