-
December 28th, 2009, 03:55 PM
#1
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.
-
December 28th, 2009, 06:15 PM
#2
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); }
-
December 28th, 2009, 08:46 PM
#3
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>
-
December 29th, 2009, 11:27 AM
#4
Re: extract data from textarea
Originally Posted by lmpb17
Something isnt right
you are goinging to need to be a more specific.
-
December 29th, 2009, 01:31 PM
#5
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
Code:
user1@email.com:pass1
user2@email.com:pass2
user3@email.com:pass3
-
December 29th, 2009, 02:08 PM
#6
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.
-
December 30th, 2009, 12:08 PM
#7
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)
-
December 30th, 2009, 01:44 PM
#8
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
)
)
Last edited by PeejAvery; December 30th, 2009 at 01:49 PM.
If the post was helpful...Rate it! Remember to use [code] or [php] tags.
-
December 30th, 2009, 02:27 PM
#9
Re: extract data from textarea
That worked perfectly. Thank you.
-
August 5th, 2010, 07:29 AM
#10
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|