Hello, we need the code for the following case: we need to replace two different letters in the word with them (the letters need to be entered manually, "m" and "n"), that is, for example: write WigMaher we get MarikPacher. I really hope for help.

2 answers 2

You can use the strstr function and pass an array as a parameter to the function:

$result = strtr('МарикПахер', ['М' => 'П', 'П' => 'М']); 

The only problem is that the case is not taken into account, that is, it will be necessary to collect an array of lowercase and then upper values ​​when entering data using the strtolower and strtoupper to do this is not difficult.

  • The result of your code: МарикМахер - Visman
  • Indeed, I forgot about the feature in case of multiple replacements, then strstr remains to solve the problem - Yaroslav Molchan

To replace characters, you can use the strtr function:

 $str = 'ПарикМахер'; echo strtr($str, ["П" => "М", "М" => "П"]); 
  • Please add a description of the code to your reply to make it easier for users to understand. To edit the answer, click Edit . You can also read tips on how to write a good answer . - From the queue of checks - Yuri
  • @Yuri, to be honest, you surprised me - what to comment on in just two lines of code? - Edward
  • Well, you know, your answer will not be deleted. It was just a tip. Usually, if there is a bare code, then he is asked to comment or describe :) - Yuri
  • Well, an example of how to comment is the second answer. ) - Yuri
  • @Yuri, thank you for the advice. - Edward