PHP str_replace是否有超过13个字符的限制?

时间:2011-10-14 18:02:54

标签: php compiler-errors str-replace

直到第13个字符被击中为止。一旦str_ireplace在cyper数组中命中“a”,str_ireplace就会停止工作。

阵列的大小是否有限制?请记住,如果键入“abgf”,我会得到“nots”,但如果我输入“abgrf”,当我得到“注释”时,我会得到“notrs”。我的大脑无法解决这个问题。

$_cypher = array("n","o","p","q","r","s","t","u","v","w","x","y","z","a","b","c","d","e","f","g","h","i","j","k","l","m");

$_needle = array("a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z");


$_decryptedText = str_ireplace($_cypher, $_needle, $_text);
echo $_decryptedText;

帮助?

4 个答案:

答案 0 :(得分:5)

使用strtrDocs

$_text = 'abgrf';

$translate = array_combine($_cypher, $_needle);

$_decryptedText = strtr($_text, $translate);

echo $_decryptedText; # notes

Demo


  

但是,有什么我做错了吗?

它将在已经替换的字符串上替换每一对,一对接一个。因此,如果您替换再次替换的角色,可能会发生这种情况:

    r -> e   e -> r
abgrf -> notes -> notrs

你的替换后你的电子更换。

答案 1 :(得分:2)

str_replace的文档中占据一席之地。即以下行:

  

因为str_replace()从左向右替换,所以在执行多次替换时,它可能会替换先前插入的值。另请参阅本文档中的示例。

所以它正如所说的那样工作。它只是进行循环替换(n - > a,然后a - > n)。

答案 2 :(得分:1)

使用str_rot13

答案 3 :(得分:0)

虽然它似乎是一个直的rot13,但如果不是,另一种选择是使用strtr()。您提供了一个字符串和一组替换对,并返回结果。