如何从HTML TextArea输入遍历PHP数组?

时间:2011-02-24 22:43:07

标签: php

每当我收到来自HTML Textarea框的输入时:

 joeschmo@gmail.com:25wfsfrs
 johnbrown@yahoo.com:324dasd
 janedoe@yahoo.com:3dsadasd

我想在冒号上使用PHP函数explode()并打印出如下列表:

Usernames:
1st Email
2nd Email
3rd Email

Passwords:
1st Pass
2nd pass
3rd Pass
到目前为止

代码:

<html>
<head>
<title>
Usernames AND Passwords!
</title>
</head>
<body>


<?php
/*Print contents of textarea for testing purposes.*/

$data = explode("\n", $_POST['uandp']);
foreach($data as $value){
    echo $value;
}

/*Split username and password*/
$string = $_POST['uandp']; 
list($string1,$string2) = explode(":",$string);  
/*$string1 = username, $string2 = password*/ 
$new_string = $string1;
$new_string2 = $string2;
/*$new_string = Username*/
/*$new_string = Password*/

echo $new_string;
echo $new_string2;
?>
</body>
</html>

不知道从哪里开始。我知道我必须将textarea表单数据存储到数组中并从那里解析它。

1 个答案:

答案 0 :(得分:3)

$uAndPs = array();

foreach (explode("\n", $_POST['uandp']) as $value){
    list($username, $password) = explode(':', $value);
    $uAndPs[$username] = $password;
}

echo "Usernames:\n"
echo join("\n", array_keys($uAndPs));

echo "Passwords:\n"
echo join("\n", $uAndPs);