如何在不替换替换子字符串的情况下替换多个子字符串?
How to replace multiple substrings without replacing replaced substrings?
帮助或协助更换我的案例中的此类变体:
$string = "This is simple string";
$search = array (
"This is simple",
"string",
"simple",
"apple"
);
$replace = array (
"This is red",
"apple",
"false",
"lemon"
);
$result = str_replace($search, $replace, $string);
Result must be: This is red apple
Not so: This is false apple OR This is red lemon OR This is false red lemon
如果在每次替换时,将更改的行切入某个变量然后稍后返回,则结果可能是正确的。但我不知道这是我的选择,但我无法实现它。
使用strtr()
:
$string = "This is simple string";
$search = array
(
"This is simple",
"string",
"simple",
"apple"
);
$replace = array
(
"This is red",
"apple",
"false",
"lemon"
);
echo strtr($string,array_combine($search, $replace));
输出:
This is red apple
重要
我必须告诉读者,这个漂亮的功能也是一个古怪的功能。如果您以前从未使用过此功能,我建议您 read the manual 以及它下面的评论。
对于这种情况很重要(而不是我的回答):
If given two arguments, the second should be an array in the form array('from' => 'to', ...). The return value is a string where all the occurrences of the array keys have been replaced by the corresponding values. The longest keys will be tried first. Once a substring has been replaced, its new value will not be searched again.
在 OP 的编码尝试中,键 ($search
) 按长度降序排列。这使函数行为与大多数人期望发生的事情保持一致。
但是,考虑一下这个演示,其中的键(及其值)被稍微打乱了:
代码:(Demo)
$string="This is simple string";
$search=[
"string", // listed first, translated second, changed to "apple" which becomes "untouchable"
"apple", // this never gets a chance
"simple", // this never gets a chance
"This is simple" // listed last, but translated first and becomes "untouchable"
];
$replace=[
"apple",
"lemon",
"false",
"This is red"
];
echo strtr($string,array_combine($search, $replace));
您可能会惊讶地发现这提供了相同的输出:This is red apple
帮助或协助更换我的案例中的此类变体:
$string = "This is simple string";
$search = array (
"This is simple",
"string",
"simple",
"apple"
);
$replace = array (
"This is red",
"apple",
"false",
"lemon"
);
$result = str_replace($search, $replace, $string);
Result must be: This is red apple
Not so: This is false apple OR This is red lemon OR This is false red lemon
如果在每次替换时,将更改的行切入某个变量然后稍后返回,则结果可能是正确的。但我不知道这是我的选择,但我无法实现它。
使用strtr()
:
$string = "This is simple string";
$search = array
(
"This is simple",
"string",
"simple",
"apple"
);
$replace = array
(
"This is red",
"apple",
"false",
"lemon"
);
echo strtr($string,array_combine($search, $replace));
输出:
This is red apple
重要
我必须告诉读者,这个漂亮的功能也是一个古怪的功能。如果您以前从未使用过此功能,我建议您 read the manual 以及它下面的评论。
对于这种情况很重要(而不是我的回答):
If given two arguments, the second should be an array in the form array('from' => 'to', ...). The return value is a string where all the occurrences of the array keys have been replaced by the corresponding values. The longest keys will be tried first. Once a substring has been replaced, its new value will not be searched again.
在 OP 的编码尝试中,键 ($search
) 按长度降序排列。这使函数行为与大多数人期望发生的事情保持一致。
但是,考虑一下这个演示,其中的键(及其值)被稍微打乱了:
代码:(Demo)
$string="This is simple string";
$search=[
"string", // listed first, translated second, changed to "apple" which becomes "untouchable"
"apple", // this never gets a chance
"simple", // this never gets a chance
"This is simple" // listed last, but translated first and becomes "untouchable"
];
$replace=[
"apple",
"lemon",
"false",
"This is red"
];
echo strtr($string,array_combine($search, $replace));
您可能会惊讶地发现这提供了相同的输出:This is red apple