替换 preg_replace 模式中的两个值

Replace two values in a preg_replace pattern

我有一个正则表达式可以在文本中搜索这样的事件:

找到

[pass id="HEH0Iu6rYl"][/pass]

正则表达式

preg_match_all('@\[pass id="(.*?)"\]\[\/pass\]@', $text, $matches);

对于所有找到的 $matches 我在我的数据库中查找“代码”,如果找到我想从数据库中填写密码,这样文本看起来像这样:[pass id="HEH0Iu6rYl"]MYPASSWORD[/pass]

函数

preg_match_all('@\[pass id="(.*?)"\]\[\/pass\]@', $docPart->getText(), $matches);

foreach ($matches[1] as $key => $match) {

    /* Try to find existing Code and Update Password */
    $password = $this->getDoctrine()->getRepository('AppBundle:Password')->findOneBy(array(
        'code' => $match,
    ));

    /* Check if it s Users own Password or User is able via AccesGroup */
    if($aG->getCanPassword() === true || $password->getUser() === $this->getUser()){
            
        /* Update the text */
        $pwText = preg_replace('#(\]).*?(\[/pass])#', $password->getPass(), $docPart->getText());
    }
}

但是替换看起来像这样:[pass id="HEH0Iu6rYl"MYPASS 我该如何更改正则表达式?

改为:

$pwText = preg_replace('#(\]).*?(\[/pass])#', ''.$password->getPass().'', $docPart->getText());
//                                            ^^^^^                    ^^^^^

</code> 包含第 1 组,即。 <code>]</code> 包含 <code>[/pass]