我有一组存储的消息(字符串),用于在MySql中通过PHP发送短信,比如说
We have received [MACHINE_NAME] for service. Your service request id is [NEW_SERVICE_ID]. Thank you.
现在我必须用机器名替换[MACHINE_NAME]
,用PHP脚本生成服务ID替换[NEW_SERVICE_ID]
。我需要多次使用str_replace()函数来替换单词吗?
答案 0 :(得分:2)
这样做
$str="We have received [MACHINE_NAME] for service. Your service request id is [NEW_SERVICE_ID]. Thank you.";
$remove = array("[MACHINE_NAME]", "[NEW_SERVICE_ID]");
$add = array("YOURMACHINE_NAME", "SERVICE_ID");
echo $onlyconsonants = str_replace($remove, $add,$str );
答案 1 :(得分:1)
不,您不必:只使用数组进行搜索和替换参数,如docs中所述:
$res = str_replace(
['[MACHINE_NAME]', '[NEW_SERVICE_ID]'],
[ $machine_name, $new_service_id ],
$source);