extract data from textarea
I have a text area form which takes data like this:
Code:
user1:pass1
user2:pass2
user3:pass3
The goal is to get this data into a mysql table whose fields are id, user, pw.
To split the lines I use:
Code:
$text = $_POST['text'];
$data = explode(explode("/\r\n|\r|\n/", $text); //split text area by line
Now I am not sure how to proceed. I need two separate arrays that will contain the user and pw values. I cant use explode like for this because it would separate them into a single array.
Re: extract data from textarea
Code:
$text = $_POST['text'];
$users = array();
$pass = array();
$data = explode(explode("/\r\n|\r|\n/", $text); //split text area by line
foreach($data as $line)
{ list($users[], $pass[]) = explode(":", $line); }
Re: extract data from textarea
Something isnt right
Code:
<?
if($_POST){
///////////////////////////////////////////////////////////////////
//connect to db
$con = mysql_connect("localhost","user1","pass1") or die(mysql_error());
mysql_select_db("myDB") or die(mysql_error());
///////////////////////////////////////////////////////////////////
//Get accounts in user:pass format and sort
$text = $_POST['text'];
$users = array();
$pass = array();
$data = explode("/\r\n|\r|\n/", $text); //split text area by line
foreach($data as $line)
{
list($users[], $pass[]) = explode(":", $line);
}
/////////////////////////////////////////////////////////////////////
//Insert into database
foreach($users as $u => $val)
{
$insert = "INSERT INTO accounts (email) VALUES ($u)";
mysql_query($insert);
}
foreach($pass as $p)
{
$insert = "INSERT INTO accounts (pw) VALUES ($p)";
mysql_query($insert);
}
//////////////////////////////////////////////////////////////////////
mysql_close($con);
echo "Data inserted <br>";
}
?>
<html>
<head>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<textarea cols="50" rows="20" name="text"></textarea>
<br>
<input type="submit" value="SUBMIT" />
</form>
</body>
</html>
Re: extract data from textarea
Quote:
Originally Posted by
lmpb17
Something isnt right
you are goinging to need to be a more specific.
Re: extract data from textarea
Sorry I must have forgot to add the problem.
Nothing is being added to the database. I tried using this to see what was being passed:
Code:
foreach($users as $u)
{
echo $u;
}
foreach($pass as $p)
{
echo $p;
}
And out of the list below only the ones in red were printed
Re: extract data from textarea
My next test would be to echo out each $line, then $text itself just so I could be sure I'm dealing with the strings I think I'm dealing with.
Re: extract data from textarea
Did as you recommended and it looks like the issue is with the way the data from the text area is posted. It comes in with whitespace, not a newline. I have tried using explode with all the whitespace codes I could find but was not able to split the string:
Code:
$data = explode("/\s+|\n|\r\n|\t|x0B| |\0/", $text)
Re: extract data from textarea
I think you are going about this wrong. Rather than using two different arrays, you should use one two dimensional array. Try the following code instead.
PHP Code:
<?php
if (isset($_POST['text'])) {
// get rid of whitespace...also, since not all OS use \r
// but everyone has \n in it, let's get rid of the \r
$text = str_replace("\r", '', trim($_POST['text']));
// now that you have your raw text
// we need an array to store the data
$data = array();
// split line by line
$lines = explode("\n", $text);
foreach ($lines as $line) {
// split the data by a colon
$parts = explode(':', $line);
// set all data into one two dimensional array
$data[] = array('user' => $parts[0], 'pass' => $parts[1]);
}
echo '<pre>';
print_r($data);
echo '</pre>';
exit;
}
?>
<html>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<textarea cols="50" rows="20" name="text"></textarea><br />
<input type="submit" value="Submit" />
</form>
</body>
</html>
That will give you the following results. Now you only have to iterate the $data array and not two separate arrays for inserting.
Code:
Array
(
[0] => Array
(
[user] => user1
[pass] => pass1
)
[1] => Array
(
[user] => user2
[pass] => pass2
)
[2] => Array
(
[user] => user3
[pass] => pass3
)
)
Re: extract data from textarea
That worked perfectly. Thank you.
Re: extract data from textarea
Thanks PeejAvery
I was able to use the same code for some csv data, I just swapped the : for a ,
Great stuff