PHP foreach array return 正则式的问题
$a1 = '/\{[A-Z]*\}/';
$a2 = array(
'{DOMEA} = 'AAAAAA';
'{DOMEB} = 'BBBBBBB';
)
$a3 = return('
<h1>title</h1>
{DOMEA}
<hr>
{DOMEB}
');
$b1 = preg_replace($a1,$a2,$a3);
怎么才实现$a2自动匹配$a3里对应的内容呢?
满脑子全是浆糊,实在想不出来,用foreach试了半天了也没弄出来,不知那位路过的PHP大大能给解解……
adgasdg
9 years, 8 months ago
Answers
因为这根本不是什么正则,只是最普通的
str_replace
罢了……
php
$a2 = array( '{DOMEA}' => 'AAAAAA', '{DOMEB}' => 'BBBBBBB', ); $a3 = ' <h1>title</h1> {DOMEA} <hr> {DOMEB} '; echo str_replace(array_keys($a2), array_values($a2), $a3);
shhfeng
answered 9 years, 8 months ago
<?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);
?>
某个安静的路人
answered 9 years, 8 months ago