我有一个正则表达式,可以在文本中搜索这样的出现:
[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
我如何更改正则表达式?