我有两个数组: $ ORGvarvals (ORGvarvals包含需要在String中替换的值)& $ varvals (varvals包含需要用字符串中的ORGvarvals替换的值) $ str (包含内容)。
这是我的$ ORGvarval:
Array ( [0] => 56,231 [1] => abc [2] => 172.0.0.1 [3] => JMeter [4] => http://172.0.0.1/file/path/ [5] => ${__time(yyyy/MM/dd HH:mm:ss,)} [6] => API [7] => Unique [8] => /home/path/to/file/filename_${__time(yyyy-MM-dd_HH-mm-ss,)}.xls [9] => C:\xls\file\path\results${__time(ddMMyyyyHHmmss,)}.csv [10] => 172.0.0.1 [11] => 9999 [12] => 22 [13] => username [14] => password [15] => /tomcat/path [16] => C:\path\to\testdata [17] => jdbc:oracle:thin:db:details [18] => username1 [19] => password1 [20] => /USSD/API/URL?parameter [21] => /EXTGW/API/URL?parameter [22] => /USSD/API/URL?parameter2 [23] => /EXTGW/API/URL?parameter [24] => /EXTGW/API/URL?parameter5 [25] => /USSD/API/URL?parameter6 [26] => 310 [27] => 1234567890 [28] => 2468 [29] => 1357 [30] => 9876543210 [31] => 100 [32] => CC [33] => 500 [34] => 50 [35] => 100 [36] => 100 [37] => /path/to/logs [38] => 1237896540 [39] => 1357 [40] => abc@7891 [41] => 0 [42] => 0 [43] => 101 [44] => dd/MM/yy [45] => ADMIN [46] => abc@7891 [47] => 1357 [48] => 3 [49] => 10 [50] => jmeter_logs )
我通过表单获取用户输入,所有值都保存在另一个变量$ varvals中,格式相同。
现在我想在$ str中逐个找到$ ORGvarvals值,并用$ varvals替换它们。
我尝试了以下两个代码:
$str=str_replace($ORGvarvals, $varvals, $str);
$str=strtr($str, array_combine($ORGvarvals, $varvals));
它们都没有按预期工作。他们正在用字符串替换多次出现。
我希望逐个找到所有ORGvarvals值并替换它们,而不会在重复值的情况下弄乱已经替换的值