php - Extract everything within brackets -
i have string of html tags inside saved in database e.g:
<p>hello {$name}, welcome {$shop_name}....</p> i want replace of tags real data, @ moment loop through available data , replace if exists.
foreach($data $key => $data){ $content = str_replace('{$'.$key.'}', $data, $content); } is there better way of doing without looping through of $data? growing on 5000 rows.
i mean possible extract variables {$name}/{$shop_name} replace on found?
you can single str_replace call.
$find = array(); $replace = array(); foreach($data $key => $data) { $find[] = "\{$" . $key . "}"; $replace[] = $data; } $content = str_replace($find, $replace, $content); unless there real performance issue, wouldn't worry much.
Comments
Post a Comment