Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 用多个源替换字符串中的多个值_Php - Fatal编程技术网

Php 用多个源替换字符串中的多个值

Php 用多个源替换字符串中的多个值,php,Php,我有一个像素像: <img src='http://www.montag.com/directory_folder/tracking_noip.php?tracking=[ID_MTG]&data=[TEL]' height='1' width='1' border='0'/> 我需要输出: <img src='http://www.montag.com/directory_folder/tracking_noip.php?tracking=4875&data

我有一个像素像:

<img src='http://www.montag.com/directory_folder/tracking_noip.php?tracking=[ID_MTG]&data=[TEL]' height='1' width='1' border='0'/>
我需要输出:

<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));
$tel=“061335721”;
$email=”Chachachou@gmail.com";
$id_mtg=“560”;
$string=“”;
$patterns=array();
$patterns[0]='/\[TEL\]/';
$patterns[1]='/\[EMAIL\]/';
$patterns[2]='/\[ID\u MTG\]/';
$replacements=array();
$replacements[2]=$id\u mtg;
$replacements[1]=$email;
$replacements[0]=电话$;
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'/>

顺序颠倒了

$str = 'hi all, I said hello';

$replace_pairs = array(
  'all' => 'everybody',
  'hello' => 'hey',
);

echo strtr($str, $replace_pairs);
这对你有帮助


这将对您有所帮助。

顺序错误,因为您以相反的顺序定义了
$replacements
。即使按降序指定索引,创建数组的顺序也会保留。用
print\r($replacements)检查这一点。如果需要以正确的顺序获取数组,可以使用
ksort()
按键进行排序

但是对于这个应用程序,
strtrtr()
str\u replace()
更有意义。确保以相同的顺序定义阵列:

$search  = array('[TEL]', '[EMAIL]', '[ID_MTG]');
$replace = array($tel, $email, $id_mtg);

$result = str_replace($search, $replace, $string);

顺序错误,因为您按相反顺序定义了
$replacements
。即使按降序指定索引,创建数组的顺序也会保留。用
print\r($replacements)检查这一点。如果需要以正确的顺序获取数组,可以使用
ksort()
按键进行排序

但是对于这个应用程序,
strtrtr()
str\u replace()
更有意义。确保以相同的顺序定义阵列:

$search  = array('[TEL]', '[EMAIL]', '[ID_MTG]');
$replace = array($tel, $email, $id_mtg);

$result = str_replace($search, $replace, $string);

如果搜索固定字符串,我认为不需要正则表达式。你试过了吗?如果你搜索固定字符串,我看不出正则表达式的必要性。你试过了吗?我在火车后看到了这个解决方案,但在我看来,它完美地完成了这个任务!我觉得我搜索得太远了^^我在火车后看到了这个解决方案,但在我看来,它完美地完成了这个任务!我觉得我搜索得太远了^^