2012-12-09 59 views
-1

我想製作一個腳本,它將從一個字符列表中取一個隨機變量。每封信之前我有+ &。這是我想出迄今PHP數組和html表單

<form action="#" method="post"> 
    <input type="text" name="text"> 
    <input type="submit" name="submit"> 
</form> 

<?php 
    if (isset($_POST['submit'])) { 
    $str = $_POST["text"]; 

    $arr1 = str_split($str); 



    foreach ($arr1 as $value) 
    { 
    $length = 1; 
    $chars = '46eab95'; 
    $charslength = strlen($chars); 
    $randomstring = ''; 
    for ($i = 0; $i < $length; $i++) $randomstring .= substr($chars, rand(0, $charslength - 1), 1); 
     echo $randomstring; 
     echo "&" . $randomstring . $value; 
    } 
    } 
?> 

,但它只是擰起來,將無法正常工作,例如,當我試圖讓它做你好這是什麼出來:6&6h4&4e4&4l6&6l5&5o

+0

您能給我們一個預期產出的例子嗎? –

+0

刪除動作中的'#',不確定表單是否已發佈。 –

+0

你還應該考慮一下這樣一個事實,即你在同一個循環中做了兩次回聲,可能是它的錯誤。 – lfxgroove

回答

1

有一點改寫 - 這是否適合你?

<?php 
$str = "hello"; 
$arr1 = str_split($str); 

foreach ($arr1 as $value) 
{ 
    $chars = '46eab95'; 
    $chars = str_split($chars); 

    $randomstring = ''; 
    foreach ($arr1 as $char) 
    { 
     $randomstring .= "&" . $chars[array_rand($chars)] . $char; 
    } 
} 

echo $randomstring; //&bh&ee&4l&9l&eo 
+0

它給出&0作爲一個選項,但它沒有在字符中列出 – user8161812

+0

對不起,我的壞。已經更新了代碼。 'array_rand'不像我想的那樣從數組中返回元素,而是返回鍵。 –