关于preg_replace()函数反向引用用法的问题。
<br />$Array1=array(<br /> array('ID'=>'1','Name'=>'小王'),<br /> array('ID'=>'2','Name'=>'小李')<br /> );<br /><br />$strA='AA Name CC DD';<br />$strB='/\s+(\w+)\s+/i';<br />$Html=preg_replace($strB, $Array1[0][‘${2}’], $strA);<br />echo $Html;<br />
$Html=preg_replace($strB, ‘${2}’, $strA);
$Array1 = array(<br /> array('ID'=>'1','Name'=>'小王'),<br /> array('ID'=>'2','Name'=>'小李')<br /> );<br /> <br />$strA = 'AA Name CC DD';<br />$strB = '/\s+(\w+)\s+/ie';<br />$Html = preg_replace($strB, '$Array1[0]["$1"]', $strA);<br />echo $Html;<br />
$Array1 = array(<br /> array('ID'=>'1','Name'=>'小王'),<br /> array('ID'=>'2','Name'=>'小李')<br /> );<br /> <br />$strA = 'AA Name CC DD';<br />$strB = '/\s+(\w+)\s+/i';<br />$Html = preg_replace_callback($strB, function($m) use ($Array1) { return $Array1[0][$m[1]];}, $strA);<br />echo $Html;