Forum Moderators: coopster
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]
$text = $_POST["text"];
$lines = explode("\r",$text);
$num = count($lines);
for($i=0;$i<$count;$i++)
{
echo 'Line '.$i.': '.$lines[$i].'<br />';
}
\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
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>
[edited by: eelixduppy at 8:54 pm (utc) on June 9, 2006]
$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);
}