<code>$a1 = '/\{[A-Z]*\}/'; $a2 = array( '{DOMEA} = 'AAAAAA'; '{DOMEB} = 'BBBBBBB'; ) $a3 = return(' <h1>title</h1> {DOMEA} <hr> {DOMEB} '); $b1 = preg_replace($a1,$a2,$a3); </code>
怎么才实现$a2自动匹配$a3里对应的内容呢?
满脑子全是浆糊,实在想不出来,用foreach试了半天了也没弄出来,不知那位路过的PHP大大能给解解……
<code>$a1 = '/\{[A-Z]*\}/'; $a2 = array( '{DOMEA} = 'AAAAAA'; '{DOMEB} = 'BBBBBBB'; ) $a3 = return(' <h1>title</h1> {DOMEA} <hr> {DOMEB} '); $b1 = preg_replace($a1,$a2,$a3); </code>
怎么才实现$a2自动匹配$a3里对应的内容呢?
满脑子全是浆糊,实在想不出来,用foreach试了半天了也没弄出来,不知那位路过的PHP大大能给解解……
<code><?php //http://3v4l.org/SHdjA $a1 = '/\{[A-Z]*\}/'; $a2 = array( '{DOMEA}'=>'AAAAAA', '{DOMEB}'=>'BBBBBBB' ); $a3 = ' <h1>title</h1> {DOMEA} <hr> {DOMEB} '; echo preg_replace_callback($a1, function($matches) use($a2) { return $a2[$matches[0]]; }, $a3); ?> </code>
http://3v4l.org/KudfG
因为这根本不是什么正则,只是最普通的str_replace
罢了……
<code>php</code><code>$a2 = array( '{DOMEA}' => 'AAAAAA', '{DOMEB}' => 'BBBBBBB', ); $a3 = ' <h1>title</h1> {DOMEA} <hr> {DOMEB} '; echo str_replace(array_keys($a2), array_values($a2), $a3); </code>
<code>php</code><code><br>$a2 = array( '{DOMEA}' => 'AAAAAA', '{DOMEB}' => 'BBBBBBB', ); $a3 = ' <h1>title</h1> {DOMEA} <hr> {DOMEB} '; echo strtr($a3,$a2); </code>