【问题标题】:How can I swap the values of two different characters in a string with PHP? A becomes B, B becomes A如何用 PHP 交换字符串中两个不同字符的值? A变成B,B变成A
【发布时间】:2019-06-02 01:07:21
【问题描述】:

我在 PHP 中有一个字符串。我想用另一个字符的值交换某个字符。如果我按照自己的方式做,A 变成 B 将用 B 替换 A,但已经存在的 B 值将保持不变。当我尝试将 B 交换为 A 时,当然有一些最初没有交换的值,因为它们已经存在了。

我试过这段代码。

$hex = "long_hex_string_not_included_here";
$hex = str_replace($a,$b,$hex);
//using this later will produced unwanted extra swaps
$hex = str_replace($b,$a,$hex);

我正在寻找一个函数来交换这些值。

【问题讨论】:

    标签: php string replace


    【解决方案1】:

    使用 Temp 值(不会出现在您的字符串中。可以是任何值):

    $temp = "_";
    $a = "a";
    $b = "b";
    $hex = "abcdabcd";
    $hex = str_replace($a,    $temp, $hex); // "_bcd_bcd"
    $hex = str_replace($b,    $a,    $hex); // "_acd_acd"
    $hex = str_replace($temp, $a,    $hex); // "bacdbacd"
    
    // Or, alternativly a bit shorter:
    $temp = "_";
    $a = "a";
    $b = "b";
    $hex = str_replace([$a, $b, $temp], [$temp, $a, $b] $hex);
    

    【讨论】:

    • 当然你必须确保字符串中不存在 C 值,否则你会得到另一个不想要的结果
    【解决方案2】:

    我们可以尝试用第三个中间值替换B,然后将所有A 替换为B,然后将标记替换回A。但这总是留下标记字符可能已经出现在字符串中的某个位置的可能性。

    更安全的方法是将输入字符串转换为字符数组,然后遍历数组,只需检查每个索引是否有AB,并进行相应的交换。

    $input = "AzzzzB";
    echo $input ."\n";
    $arr = str_split($input);
    
    for ($i=0; $i < count($arr); $i++) {
        if ($arr[$i] == 'A') {
            $arr[$i] = 'B';
        }
        else if ($arr[$i] == 'B') {
            $arr[$i] = 'A';
        }
    }
    $output = implode('', $arr);
    echo $ouput;
    
    AzzzzB
    BzzzzA
    

    另请注意,这种方法很有效,并且只需要遍历输入字符串一次。

    【讨论】:

    • 虽然这没有得到字符串中存在中间值的问题,但这不适用于字符长度> 1。我很好奇这是如何执行的。我的中间确实有一个额外的替换,它也是字符串操作。不过我喜欢这个答案,+1。
    【解决方案3】:

    只需使用strtr。这就是它的设计目的:

    $string = "abcbca";
    echo strtr($string, array('a' => 'b', 'b' => 'a'));
    

    输出:

    bacacb
    

    这里有帮助的关键功能是当strtr 以两个参数形式被调用时:

    一旦一个子字符串被替换,它的新值将不再被搜索。

    这就是阻止 ab 替换然后再次被 a 替换的原因。

    Demo on 3v4l.org

    【讨论】:

    • 我答对了面试题,你给出了最佳答案+1。
    【解决方案4】:

    另一种方法是str_split 字符串并使用array_map 测试每个字符。如果a,则返回b,反之亦然。否则返回原始值。

    $hex = "abba test baab";
    $hex = array_map(function ($x) {
        return ($x === 'a') ? 'b' : (($x === 'b') ? 'a' : $x);
    }, str_split($hex));
    
    echo implode('', $hex);
    

    结果

    baab test abba
    

    Demo

    【讨论】:

      【解决方案5】:

      我的解决方案适用于子字符串。代码不是很清楚,但我想给你一个思路。

      $html = "dasdfdasdff";
      $firstLetter = "d";
      $secondLetter = "a";
      $firstLetterPositions = array();
      $secondLetterPositions = array();
      
      $lastPos = 0;
      while (($lastPos = strpos($html, $firstLetter, $lastPos))!== false) {
          $firstLetterPositions[] = $lastPos;
          $lastPos = $lastPos + strlen($firstLetter);
      }
      
      $lastPos = 0;
      while (($lastPos = strpos($html, $secondLetter, $lastPos))!== false) {
          $secondLetterPositions[] = $lastPos;
          $lastPos = $lastPos + strlen($secondLetter);
      }
      
      for ($i = 0; $i < count($firstLetterPositions); $i++) {
          $html = substr_replace($html, $secondLetter, $firstLetterPositions[$i], count($firstLetterPositions[$i]));
      }
      
      for ($i = 0; $i < count($secondLetterPositions); $i++) {
          $html = substr_replace($html, $firstLetter, $secondLetterPositions[$i], count($secondLetterPositions[$i]));
      }
      var_dump($html);
      

      【讨论】:

      • 当你用另一个长度值替换一个字符串时,这不会失败吗?例如。 'a' 与 'bb'?与 3 个字符串替换相比,此解决方案相当过分,并且没有提供很多价值。
      猜你喜欢
      • 1970-01-01
      • 2015-01-31
      • 2010-12-21
      • 2022-11-29
      • 1970-01-01
      • 1970-01-01
      • 2023-03-04
      • 2015-04-08
      相关资源
      最近更新 更多