Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP template string replace

Tags:

php

just wondering what's the best approach for template String replace.

preg_replace('/{%(\S+)%}/', 'bill', $tableOutput);

With the preg_replace, if there are lots of string needs to be replace, the function will be called many times depend on the number of string needs to be replace. Just wondering if there is a way can look though string once and replace accordingly?

Thanks

like image 921
Bill Avatar asked Aug 17 '26 15:08

Bill


1 Answers

Use the array syntax of preg_replace...?

preg_replace(array('/pattern1/', '/pattern2/'), array('replacement1', ...), ...)
like image 120
deceze Avatar answered Aug 20 '26 06:08

deceze