Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用PHP交换字符串中两个不同字符的值?A变成B,B变成A_Php_String_Replace - Fatal编程技术网

如何使用PHP交换字符串中两个不同字符的值?A变成B,B变成A

如何使用PHP交换字符串中两个不同字符的值?A变成B,B变成A,php,string,replace,Php,String,Replace,我有一个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_repl

我有一个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);
我正在寻找一个函数来交换这些值。

使用一个临时值(它不会出现在字符串中。可以是任何值):

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

一种更安全的方法是将输入字符串转换成一个字符数组,然后在数组中遍历,只需检查每个索引中的
A
B
,然后进行相应的交换

$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
$input=“AzzzzB”;
回显$input。“\n”;
$arr=str_分割($input);
对于($i=0;$i
还要注意,这种方法是有效的,只需要沿着输入字符串走一次。

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

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

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

替换子字符串后,将不再搜索其新值

这就是阻止被
b
替换的
a
再次被
a
替换的原因

另一种方法是使用字符串并对每个字符进行测试。如果
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

我的解决方案适用于子字符串。代码不清楚,但我想向您展示一种思维方式

$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);
$html=“dasdfdasdff”;
$firstLetter=“d”;
$secondLetter=“a”;
$firstLetterPositions=array();
$secondLetterPositions=array();
$lastPos=0;
而($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);
}
对于($i=0;$i
但当然,您必须确保字符串中不存在C值,否则您将得到另一个不需要的结果。尽管这没有导致字符串中存在中间值的问题,但这对charlength>1不起作用。我很好奇这是怎么回事。我的中间版本确实有一个额外的替换位,它也是字符串操作。我喜欢这个答案,+1。我确定了面试问题,你给出了最好的答案+1。当你用另一个长度值替换字符串时,这不是失败了吗?例如,“a”和“bb”?与3个字符串替换相比,这个解决方案是相当过分的,并且没有提供太多的价值。
$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);