Does PHP str_replace have a greater than 13 character limit?

倖福魔咒の 提交于 2019-12-02 05:47:08

问题


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?


回答1:


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.




回答2:


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




回答3:


Use str_rot13




回答4:


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.



来源:https://stackoverflow.com/questions/7771658/does-php-str-replace-have-a-greater-than-13-character-limit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!