Forum Moderators: coopster

Message Too Old, No Replies

reading data from a textareaa box

reading data from a textareaa box

         

vivek avasthi

6:32 am on Jun 8, 2006 (gmt 0)

10+ Year Member



hi, i have a textarea box ..i want to read data line by line from there ... how can i do this ..

eelixduppy

10:51 am on Jun 8, 2006 (gmt 0)



What are you trying to accomplish? Depending on this, the solution can be different ;)

vivek avasthi

11:20 am on Jun 8, 2006 (gmt 0)

10+ Year Member



i have a text area storing value like this

example@gmail.com,vivek,m-67 laxminagar
example@gmail.com,vivek,m-67 laxminagar
example@gmail.com,vivek,m-67 laxminagar
example@gmail.com,vivek,m-67 laxminagar
example@gmail.com,vivek,m-67 laxminagar
example@gmail.com,vivek,m-67 laxminagar
example@gmail.com,vivek,m-67 laxminagar
example@gmail.com,vivek,m-67 laxminagar

now i want to store these value separately in email,name & address field ..i also want to decide my separator..

[edited by: jatar_k at 3:09 pm (utc) on June 8, 2006]
[edit reason] examplified [/edit]

eelixduppy

3:40 pm on Jun 8, 2006 (gmt 0)



How about this, after the form is submitted?

$text = $_POST["text"];
$lines = explode("\r",$text);
$num = count($lines);
for($i=0;$i<$count;$i++)
{
echo 'Line '.$i.': '.$lines[$i].'<br />';
}

grandpa

6:57 pm on Jun 8, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'd recommend that you update this line:
for($i=0;$i<$count;$i++)

should be

for($i=0;$i<$num;$i++)

eelixduppy

8:12 pm on Jun 8, 2006 (gmt 0)



oops! Thanks grandpa. I should really look carefully over my code before i post ;)

RogueDogg

5:39 pm on Jun 9, 2006 (gmt 0)

10+ Year Member



So as some of you know i'm trying to learn this myself. So if I'm reading this correctly your counting the lines till you reach the last "r" and then moving down to the next line?

If I'm some what close wouldn't this be a problem with more than 1 'r' in that line? Maybe I'm way off base here :)

eelixduppy

6:44 pm on Jun 9, 2006 (gmt 0)



/r is practically equivalent to hitting the 'enter' key on your keyboard. So the script takes each line in the textbox and prints them out.

dreamcatcher

8:07 pm on Jun 9, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually new lines in a textarea are formatted as such:

\r\n

So, you might need to use the newline character too. To understand eelixduppy's code a little better, try viewing the array before you process the loop to see how its working:

So, after this:
$lines = explode("\r",$text);

add this:

print_r($lines);exit;

Each line is in a seperate indice in the array created by explode() [uk.php.net], which created an array of strings. You can then access each indice seperately or loop through them as the example shows:

dc

eelixduppy

8:15 pm on Jun 9, 2006 (gmt 0)




Actually new lines in a textarea are formatted as such:

\r\n

Thanks for the input, I didn't actually know that. I tried both ways and they both seem to work on my computer ;)

<edit>
If they are formatted as \r\n (which i now know they are), then using just \r works because it encounters \r at the same place that it encounters \r\n, and it works using \r\n because that is actually how if is formatted.
</edit>

jatar_k

8:50 pm on Jun 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



but, if you use \r then each line will be preceded by a newline, which could cause issues displaying, or not.

It is always good to control the data and make sure you know exactly what you are getting on the other side ;)

eelixduppy

8:52 pm on Jun 9, 2006 (gmt 0)



I didn't think of that. When it gets printed to the browser you cannot notice it; it's only when you view the source that you can see. Thanks.

[edited by: eelixduppy at 8:54 pm (utc) on June 9, 2006]

coopster

8:53 pm on Jun 9, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



The newline may actually be just "\r" if I remember correctly, if it is a MAC OS <= 9. You could always explode on the possibility of either or both being there if you used a regular expression. And trim any excess whitespace on the values after that.

$text = $_POST['text']; 
$data = array_map [php.net]('trim [php.net]', preg_split [php.net]("/\r\n¦\n¦\r/", $text, -1, PREG_SPLIT_NO_EMPTY));
$query = "INSERT INTO mytable (email, name, address) \n";
$values = ''; // initialize
foreach [php.net] ($data as $value) {
list [php.net]($email, $name, $address) = explode [php.net](',', $value, 3);
// Scrub the data (make sure it is a valid email, etc.)
// Then escape your data (MySQL example shown):
//$email = mysql_real_escape_string [php.net]($email);
//$name = mysql_real_escape_string($name);
//$address = mysql_real_escape_string($address);
$values .= "('$email', '$name', '$address'),\n";
}
if ($values = rtrim [php.net]($values)) {
$query .= "VALUES \n" . rtrim [php.net]($values, ',');
print "\n" . $query;
// mysql_query($query);
}

Don't forget that the forum breaks the pipe symbol (¦) so you will need to rekey them if you copy/paste this code.