如何替换” A"与" D"," B"与" E"," C"与"餐饮QUOT ;,。 。 。英寸×"与" A"," Y"与" B"和" Z"与" C"? [英] How to replace "a" with "d", "b" with "e", "c" with "f", . . . "x" with "a", "y" with "b", and "z" with "c"?
问题描述
我想执行一个字符串此搜索和替换,比如密码
。
I would like to perform this search and replacement on a string, say password
.
你可以从这个问题明白了,更换后的字符串应该成为 sdvvzrug
。
As you can understand from the question, after replacement the string should become sdvvzrug
.
但遗憾的是以下code输出 bbbbcaab
:
But unfortunately the following code outputs bbbbcaab
:
$search = range("a", "z");
$replace = array_merge(range("d", "z"), range("a", "c"));
echo str_replace($search, $replace, "password");
什么可能导致此问题?起初我怀疑是使用数组的 str_replace转换
,所以我用了一个循环,并尝试一次更换一个字符:
What may be causing the issue here? At first I suspected the use of arrays in str_replace
, therefore I used a loop and tried replacing one character at a time:
$search = range("a", "z");
$replace = array_merge(range("d", "z"), range("a", "c"));
$str = "password";
for($i = 0; $i < 26; $i++)
{
$str = str_replace($search[$i], $replace[$i], $str);
}
echo $str;
要我彻底失望,结果仍然是相同的。我在做什么错了,我应该怎么实现正确的结果?
To my utter dismay, the result remained the same. What am I doing wrong and how should I achieve the correct results?
推荐答案
其他答案已经解释你如何能做到这一点,所以我将重点放在了我在做什么了?一部分。
Other answers have explained how you can do this, so I will focus on the "What am I doing wrong?" part.
所以,你的为
-loop相当于这样的:
So, your for
-loop is equivalent to this:
$str = str_replace('a', 'd', $str);
$str = str_replace('b', 'e', $str);
$str = str_replace('c', 'f', $str);
$str = str_replace('d', 'g', $str);
$str = str_replace('e', 'h', $str);
$str = str_replace('f', 'i', $str);
$str = str_replace('g', 'j', $str);
// ...
$str = str_replace('z', 'c', $str);
正如你所看到的,这种字母通过扫荡的: A
获取与替换ð
,后来 D
被替换先按g
,后来先按g
变与Ĵ替换
。问题是,当你与先按g
替换 D
,你这样做,无论是否<$的C $ C> D 重新presents什么原是 D
或什么最初是一个 A
。每个 str_replace转换
被丢弃的信息;第一行后,你不能告诉字符串 DDD
是否原是添加
,或者原来父亲
,当您更改 D
到先按g
,你得到 GGG
而不是 DGG
或 GDG
或诸如此类的东西。
As you can see, this sort of "sweeps through" the alphabet: a
gets replaced with d
, and later d
gets replaced g
, and later g
gets replaced with j
. The problem is that when you're replacing d
with g
, you're doing it regardless of whether the d
represents what was originally a d
or what was originally an a
. Each str_replace
is discarding information; after the first line, you can't tell whether the string ddd
was originally add
, or originally dad
, and when you change d
to g
, you get ggg
instead of dgg
or gdg
or whatnot.
原因的版本只是一个单一的 str_replace转换
有同样的结果是 str_replace转换
做同样的事情,你的循环:它只是搜索阵列上迭代,进行替换,并不能保证它永远不会重新替换了一子。这就是为什么你需要使用的方法,如 strtr函数的效率
就是专门为此而设计的。
The reason the version with just a single str_replace
had the same result is that str_replace
does the same thing as your loop: it just iterates over the search-array, performing the replacements, and doesn't ensure that it never "re-replaces" a substring. That's why you need to use a method such as strtr
that's specifically designed for this purpose.
这篇关于如何替换&rdquo; A&QUOT;与&QUOT; D&QUOT;,&QUOT; B&QUOT;与&QUOT; E&QUOT;,&QUOT; C&QUOT;与&QUOT;餐饮QUOT ;,。 。 。英寸×&QUOT;与&QUOT; A&QUOT;,&QUOT; Y&QUOT;与&QUOT; B&QUOT;和&QUOT; Z&QUOT;与&QUOT; C&QUOT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!