2012-01-28 9 views
0

私は完璧に動作するこのランダムパスワードスクリプトを書いています。PHPはランダムなパスワードスクリプトで発音を確認します

ランダムパスワードには、phaneticアルファベットを示す以下の行を追加したいと思いますが、

これを実装するにはどうすればよいでしょうか?

<?php 
function random_readable_pwd($length=10){ 

    // the wordlist from which the password gets generated 
    // (change them as you like) 
    $words = 'AbbyMallard,AbigailGabble,AbisMal,Abu,Adella,TheAgent,AgentWendyPleakley,Akela,AltheAlligator'; 

    $phonetic = array("a"=>"alfa","b"=>"bravo","c"=>"charlie","d"=>"delta","e"=>"echo","f"=>"foxtrot","g"=>"golf","h"=>"hotel","i"=>"india","j"=>"juliett","k"=>"kilo","l"=>"lima","m"=>"mike","n"=>"november","o"=>"oscar","p"=>"papa","q"=>"quebec","r"=>"romeo","s"=>"sierra","t"=>"tango","u"=>"uniform","v"=>"victor","w"=>"whisky","x"=>"x-ray","y"=>"yankee","z"=>"zulu"); 

    // Split by ",": 
    $words = explode(',', $words); 
    if (count($words) == 0){ die('Wordlist is empty!'); } 

    // Add words while password is smaller than the given length 
    $pwd = ''; 
    while (strlen($pwd) < $length){ 
     $r = mt_rand(0, count($words)-1); 
     $pwd .= $words[$r]; 
    } 

    $num = mt_rand(1, 99); 
    if ($length > 2){ 
     $pwd = substr($pwd,0,$length-strlen($num)).$num; 
    } else { 
     $pwd = substr($pwd, 0, $length); 
    } 

    $pass_length = strlen($pwd); 
    $random_position = rand(0,$pass_length); 

    $syms = "[email protected]#%^*()-?"; 
    $int = rand(0,9); 
    $rand_char = $syms[$int]; 

    $pwd = substr_replace($pwd, $rand_char, $random_position, 0); 

    return $pwd; 
} 
?> 
<html><head><title>Password generator</title></head> 
<body><p><?php echo random_readable_pwd(10); ?></p></body> 
</html> 

例えば出力:!

AltキーheAll87

ALFAのリマのタンゴ!ホテルエコーアルファリマリマ8 7

答えて

3

あなたは、生成されたパスワードを文字ごとにループして、そのような音標的なストリングを構築する必要があります。例えば

(下の例では、テストされていませんが、あなたがそれに近づくことができる方法の理解を与える必要があり、あなたのコードやニーズにカスタマイズ):

$password = "aBcDefG"; 
$phonetics = array("a"=>"alfa","b"=>"bravo","c"=>"charlie","d"=>"delta","e"=>"echo","f"=>"foxtrot","g"=>"golf","h"=>"hotel","i"=>"india","j"=>"juliett","k"=>"kilo","l"=>"lima","m"=>"mike","n"=>"november","o"=>"oscar","p"=>"papa","q"=>"quebec","r"=>"romeo","s"=>"sierra","t"=>"tango","u"=>"uniform","v"=>"victor","w"=>"whisky","x"=>"x-ray","y"=>"yankee","z"=>"zulu"); 
$phonetic = array(); 
for ($i = 0; $i < strlen($password); $i++) { 
    $char = substr($password, $i, 1); 
    $phonetic[] = (ctype_upper($char) ? strtoupper(strtr(strtolower($char), $phonetics)) : strtolower(strtr($char, $phonetics))); 
} 
$phonetic = join(' ', $phonetic); 
echo $phonetic; 

EDIT私のコードは、故障した、私はそれを更新し、それをテストしました。出力は次のとおりです:alfa BRAVO charlie DELTA echo foxtrot GOLF

+0

ありがとう、私は文字列の長さを使用して各文字をループする必要があることを理解します。私が理解できない難しい部分は、表音的な配列に置き換えて、ケースをテストすることです。 –

+0

@JohnMagnolia上記の編集を参照してください! :) –

+0

初めはすごくうまくいったよ、ありがとう。それぞれの文字を特定するためにsubstrに$ iを使用した方法がわかります。 –