How to replace a letter in a string with a new letter that will not be updated

后端 未结 3 609
攒了一身酷
攒了一身酷 2020-12-12 02:08

Lets say I have some code:

$text = $_POST[\'secret\'];

$replaces = array(
        \'a\' => \'s\',
        \'b\' => \'n\',
        \'c\' => \'v\',
          


        
3条回答
  •  Happy的楠姐
    2020-12-12 02:20

    In the PHP Manual it's clearly explained your problem, at the end of the page they advice to use strtr() which does exactly what you want.

    Replace

      $text = str_replace(array_keys($replaces),array_values($replaces),$text);
    

    with

      $text = strtr($text,$replaces);
    

    which does exactly what you want, it replaces one character with another character.

    The documentation of strtr() is here: http://www.php.net/manual/en/function.strtr.php

提交回复
热议问题