Having this array:
$arr['id']=150;
$arr['contenido']="un proyecto";
$arr['foto']="una foto";
This works very good
$text = 'estamos en el registro {id} cuyo contenido es {contenido} por lo que veremos su {foto}';
$text = preg_replace('/{(.*?)}/', '$1', $text);
echo $text;
//print:
//estamos en el registro id cuyo contenido es contenido por lo que veremos su foto
I understand that $1 y the value enclosed by { and } but I need replace with the value of array that match with the key. I trying this
$text2 ='estamos en el registro {id} cuyo contenido es {contenido} por lo que veremos su {foto}';
$text2 = preg_replace('/{(.*?)}/', $arr['$1'], $text2);
echo $text2;
//print
estamos en el registro cuyo contenido es por lo que veremos su
but this no print anythig in the pos of {key}, how I get impresed the array value referenced by the key yn {}.
preg_replace_callback(
'/{(.*?)}/',
function (array $m) use ($arr) { return $arr[$m[1]]; },
$text2
)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.