Ray Avary
Ray Avary

Reputation: 3

How to traverse PHP Array from HTML TextArea Input?

Whenever I receive input such as from a HTML Textarea box:

 [email protected]:25wfsfrs
 [email protected]:324dasd
 [email protected]:3dsadasd

I want to use the PHP function explode() on the colon and print out a list such as:

Usernames:
1st Email
2nd Email
3rd Email

Passwords:
1st Pass
2nd pass
3rd Pass

Code so far:

<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>

Not sure where to go from here. I know I have to store the textarea form data into an array and parse it from there.

Upvotes: 0

Views: 1081

Answers (1)

deceze
deceze

Reputation: 522155

$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);

Upvotes: 3

Related Questions