我有像这样的像素:
<img src='http://www.montag.com/directory_folder/tracking_noip.php?tracking=[ID_MTG]&data=[TEL]' height='1' width='1' border='0'/>
我需要用我从脚本中收到的值替换[TEL]和[ID_MTG]键。例如,如果我有:
$tel = "0613321223";
$id_mtg = "4875";
$email = "thatmail@gmail.com";
我需要输出:
<img src='http://www.montag.com/directory_folder/tracking_noip.php?tracking=4875&data=0613321223' height='1' width='1' border='0'/>
如果我有:
<img src='http://www.montag.com/directory_folder/tracking_noip.php?tracking=[ID_MTG]&data=[EMAIL]' height='1' width='1' border='0'/>
我需要输出:
<img src='http://www.montag.com/directory_folder/tracking_noip.php?tracking=4875&data=thatmail@gmail.com' height='1' width='1' border='0'/>
我有:
$tel = "0613357221";
$email = "Chachachou@gmail.com";
$id_mtg = "560";
$string = "<img src='http://www.montag.com/directory_folder/tracking_noip.php?tracking=[TEL]&data=[ID_MTG]' height='1' width='1' border='0'/>";
$patterns = array();
$patterns[0] = ' /\[TEL\]/ ';
$patterns[1] = ' /\[EMAIL\]/ ';
$patterns[2] = ' /\[ID_MTG\]/ ';
$replacements = array();
$replacements[2] = $id_mtg;
$replacements[1] = $email;
$replacements[0] = $tel;
echo htmlentities(preg_replace($patterns, $replacements, $string));
但我的字符串是:
<img src='http://www.montag.com/directory_folder/tracking_noip.php?tracking=520&data=0613357221' height='1' width='1' border='0'/>
订单相反。
答案 0 :(得分:3)
$str = 'hi all, I said hello';
$replace_pairs = array(
'all' => 'everybody',
'hello' => 'hey',
);
echo strtr($str, $replace_pairs);
这会对你有帮助。
答案 1 :(得分:1)
订单错误,因为您以相反的顺序定义了$replacements
。即使您按降序指定了索引,也会保留您创建数组的顺序。请使用print_r($replacements);
进行检查。如果需要,您可以使用ksort()
按键排序,以便以正确的顺序获取数组。
但对于此应用,strtr()
或str_replace()
更有意义。确保以相同的顺序定义数组:
$search = array('[TEL]', '[EMAIL]', '[ID_MTG]');
$replace = array($tel, $email, $id_mtg);
$result = str_replace($search, $replace, $string);