Maison >développement back-end >tutoriel php >关于preg_replace()函数反向引用用法的问题。
$Array1=array( array('ID'=>'1','Name'=>'小王'), array('ID'=>'2','Name'=>'小李') );$strA='AA Name CC DD';$strB='/\s+(\w+)\s+/i';$Html=preg_replace($strB, $Array1[0][‘${2}’], $strA);echo $Html;
$Html=preg_replace($strB, ‘${2}’, $strA);
对于 php5.5 一下,可以
$Array1 = array( array('ID'=>'1','Name'=>'小王'), array('ID'=>'2','Name'=>'小李') ); $strA = 'AA Name CC DD';$strB = '/\s+(\w+)\s+/ie';$Html = preg_replace($strB, '$Array1[0]["$1"]', $strA);echo $Html;
$Array1 = array( array('ID'=>'1','Name'=>'小王'), array('ID'=>'2','Name'=>'小李') ); $strA = 'AA Name CC DD';$strB = '/\s+(\w+)\s+/i';$Html = preg_replace_callback($strB, function($m) use ($Array1) { return $Array1[0][$m[1]];}, $strA);echo $Html;