如何更换&到和在PHP中

时间:2012-07-28 14:26:59

标签: php

我想将&,%,$等特殊字符替换为字符串。

我有一个字符串s1 = Trinidad&多巴哥,我想取代'&'到'和' 例如:s1 = Trinidad&多巴哥应该被替换为s1 =特立尼达和多巴哥。

2 个答案:

答案 0 :(得分:3)

echo str_replace( "&", "and", 'Trinidad & Tobago');
//"Triniad and Tobago"

答案 1 :(得分:2)

使用str_replace

$s1 = str_replace('&', 'and', $s1);

如果需要替换多个字符,str_replace接受一个数组作为输入(用于搜索和替换):

$s1 = str_replace(array('&', '%', '$'), array('and', 'percent', 'dollar'), $s1);