dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: extract data from textarea

  1. #1
    Join Date
    Mar 2007
    Posts
    72

    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.

  2. #2
    Join Date
    Nov 2009
    Posts
    31

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

  3. #3
    Join Date
    Mar 2007
    Posts
    72

    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>

  4. #4
    Join Date
    Nov 2009
    Posts
    31

    Re: extract data from textarea

    Quote Originally Posted by lmpb17 View Post
    Something isnt right
    you are goinging to need to be a more specific.

  5. #5
    Join Date
    Mar 2007
    Posts
    72

    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

  6. #6
    Join Date
    Nov 2009
    Posts
    31

    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.

  7. #7
    Join Date
    Mar 2007
    Posts
    72

    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)

  8. #8
    PeejAvery's Avatar
    PeejAvery is offline Super Moderator Power Poster
    Join Date
    May 2002
    Posts
    10,943

    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.

  9. #9
    Join Date
    Mar 2007
    Posts
    72

    Re: extract data from textarea

    That worked perfectly. Thank you.

  10. #10
    Join Date
    Aug 2010
    Posts
    1

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)