2

Where I could find a case-insensitive version of strtr?

strtr is overloaded, I am talking about the following one

string strtr ( string $str , array $replace_pairs )
user229044
  • 232,980
  • 40
  • 330
  • 338
fbiville
  • 8,407
  • 7
  • 51
  • 79
  • @c69, read function's name one more time. It's `strtr`, not `strstr`. – binaryLV Sep 23 '11 at 14:14
  • @binaryLV damn php and its function names >_< they are not very reader friendly – c69 Sep 23 '11 at 14:17
  • @c69, it's not only PHP. C standard library also has awesome names for functions. Take a look at [string.h](http://en.wikipedia.org/wiki/String.h#Functions) (`strcspn()`?!), for example ;) – binaryLV Sep 23 '11 at 14:26

1 Answers1

7

Use str_ireplace:

str_ireplace(array_keys($replace_pairs), array_values($replace_pairs), $str);
Arnaud Le Blanc
  • 98,321
  • 23
  • 206
  • 194
  • works (almost) perfectly! The only minor issue is that uppercase characters are turned lowercase... But I can live with that, I guess. – fbiville Sep 23 '11 at 13:07
  • 1
    `str_ireplace(array('A'), array('Z'), "ABcd")` returns `ZBcd`, it doesn't change the case of the input string. – Arnaud Le Blanc Sep 23 '11 at 13:09
  • str_ireplace(array('a'),array('z'), 'ABcd'); does ;) – fbiville Sep 23 '11 at 15:18
  • obviously, it replaces by what you tell him to replace with ;) do `str_replace(array('a','A'),array('z','Z'), 'ABcd');` instead – Arnaud Le Blanc Sep 23 '11 at 17:00
  • Yes I know. However, I do not have the knowledge of the string I want to process. If I want to replace a whole set of words, I should generate all combination of lower/upper-case characters ? Imagine with only 1 word of three letters: bob, boB, bOb, bOB, Bob, BoB, BOb, BOB. The problem is that I've got n words of different length. Generating all the possible combinations just to preserve the case seems to be a big overhead. – fbiville Sep 24 '11 at 09:14
  • And with non-western alphabets, I am not even how case should be handled. – fbiville Sep 24 '11 at 09:18
  • OK: http://stackoverflow.com/questions/2750736/case-insensitive-highlighting-in-php seems to address this secondary issue. I still consider your answer valid anyway :) – fbiville Sep 24 '11 at 09:22
  • Remember that it poses a problem when you replace all number for example, then you can't decipher it anymore. It replaces sequentually instead of at the same time, like strtr does do! – twicejr Nov 14 '13 at 19:36
  • Note that `str_replace()`/`str_ireplace()` are functionally different to `strtr()` in how they perform replacements. If you are using the former with user supplied values you may end up with a code-injection vulnerability. – Courtney Miles Aug 08 '17 at 01:26
  • Problem is that str_replace ans str_ireplace is 4 times slower then strtr – Shakeel Ahmed Aug 25 '18 at 11:31
  • This is a nice answer as it uses the same format array as strtr thanks to array_keys and array_values. I mention it because I started to write a conversion function before I realised. – MortimerCat Jul 10 '20 at 12:23