开发者

Does PHP str_replace have a greater than 13 character limit?

开发者 https://www.devze.com 2023-04-13 06:56 出处:网络
This works up until the 13th character is hit. Once the str_ireplace hits \"a\" in the cyper array, the str_ireplace stops working.

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?


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.


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).


Use str_rot13


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消