-4

文字列内の 2 文字を他の 2 文字と交換したい。

開始文字列 = "`bHello `!how `Qare `%you."

ランダム文字列 = "1234567890abcdefghijklmnopqrstuvwxyz!£$%^&@"

どうすれば `b` を交換できますか! `Q `% はランダムなものなので、このようになります

最終結果文字列 = "`4Hello `^how `$are `@you."

私はこれまでにこれを試しました

ここまでやってみた

$out = "`vHow `!are `@you."

$patterns = array("`1","`J","`2","`3","`4","`5","`6","`7","`!", "`$","`%","`^","`&","`)","`~","`#","`@","`q","`e", "`y","`t","`p","`j","`k","`l","`M","`x","`v","`m", "`Q"、"`E"、"`R"、"`T"、"`Y"、"`P"、"`G"、"`K"、"`L"、"`X"、 "`V");

$pretest = array("`1","`J","`2","`3","`4","`5","`6","`7","`!", "`$","`%","`^","`&","`)","`~","`#","`@","`q","`e", "`y","`t","`p","`j","`k","`l","`M","`x","`v","`m", "`Q"、"`E"、"`R"、"`T"、"`Y"、"`P"、"`G"、"`K"、"`L"、"`X"、 "`V");

$tempstr = $pretest[rand(0, strlen($pretest)-1)];

$substs = "`".$tempstr;

$out = preg_replace($patterns, $substs, $out);

ただし、最終結果は

$out = "`%How `%are `%you."

ランダムに 1 つだけ選択し、すべてをそのランダムに変更します。

4

1 に答える 1

0
<?php
function randomChar() { 
    $rand = "1234567890abcdefghijklmnopqrstuvwxyz";
    return substr($rand, rand(0, strlen($rand)), 1);  
} 

echo preg_replace_callback("/`./", 'randomChar', "`bHello `!how `Qare `%you.");
于 2013-03-12T16:12:18.607 に答える