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

发布于 2024-12-10 02:57:15 字数 539 浏览 0 评论 0原文

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

数组的大小有限制吗?请记住,如果输入“abgf”,我会得到“nots”,但如果我在应该得到“notes”时输入“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;

帮助?

This works up until the 13th character is hit. Once the str_ireplace hits "a" in the cyper array, the str_ireplace stops working.

Is there a limit to how big the array can be? Keep in mind if type "abgf" i get "nots", but if I type "abgrf" when I should get "notes" I get "notrs". Racked my brain cant figure it out.

$_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;

Help?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

提赋 2024-12-17 02:57:15

使用 strtr文档< /sup>

$_text = 'abgrf';

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

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

echo $_decryptedText; # notes

演示


但是,我是不是做错了什么?

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

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

您的 e 替换在您的 r 替换之后进行。

Use strtrDocs:

$_text = 'abgrf';

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

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

echo $_decryptedText; # notes

Demo


But, was there something I was doing wrong?

It will replace each pair, one pair after the other on the already replaced string. So if you replace a character that you replace again, this can happen:

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

Your e-replacement comes after your r-replacement.

羁绊已千年 2024-12-17 02:57:15

查看 str_replace 的文档。即以下行:

由于 str_replace() 从左到右替换,因此在进行多次替换时,它可能会替换先前插入的值。另请参阅本文档中的示例。

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

Take a peak at the docs for str_replace. Namely the following line:

Because str_replace() replaces left to right, it might replace a previously inserted value when doing multiple replacements. See also the examples in this document.

So it's working as told. It's just doing a circular replacement (n -> a, then a -> n).

风吹雪碎 2024-12-17 02:57:15

虽然它看起来是一个直接的 rot13,但如果不是,另一种选择是使用 strtr()。您提供一个字符串和一个替换对数组,然后返回结果翻译。

although it appears to be a straight rot13, if it is not, another option is to use strtr(). You provide a string and an array of replacement pairs and get the resulting translation back.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文