And*_*ndy 3 php string str-replace
替换PHP字符串中的一组短标签的最佳方法是什么,例如:
$return = "Hello %name%, thank you for your interest in the %product_name%. %representative_name% will contact you shortly!";
Run Code Online (Sandbox Code Playgroud)
我将定义%name%是某个字符串,来自数组或对象,例如:
$object->name;
$object->product_name;
Run Code Online (Sandbox Code Playgroud)
等等..
我知道我可以在字符串上多次运行str_replace,但我想知道是否有更好的方法.
谢谢.
Jon*_*ram 14
如果您知道要替换的占位符,str_replace()似乎是一个理想的选项.这只需要运行一次而不是多次.
$input = "Hello %name%, thank you for your interest in the %product_name%. %representative_name% will contact you shortly!";
$output = str_replace(
array('%name%', '%product_name%', '%representative_name%'),
array($name, $productName, $representativeName),
$input
);
Run Code Online (Sandbox Code Playgroud)