Вопрос

I need a user to be able to enter data into a textarea in the following format.

name,email@email.com

name,email@email.com

name,email@email.com

The information would then be sent to the mysql database. My issue is that I want to place the proper name into the first column and the proper email into the second column. Then, when I encounter a line break I would like it to start a new row.

Can anyone help with this? Thanks!

Это было полезно?

Решение

As others have said you can use explode to get the results into an array however, the problem with this approach is there is no guarantee the user will enter the information in the correct order.

What I would do is this. It's still not fool proof (And using separate inputs would be better) but it's better.

<?php

$textareastring = $_POST['textareaname'];

$array = explode("\n",$textareastring );

foreach($array as $value) {
 $data = explode(',',$value);
 if (strpos($data[1], '@'))
 {
   $name = $data[0];
   $email = $data[1];
 }else{
   $name = $data[1];
   $email = $data[0];
 }
}
?>

Другие советы

I would use explode() to separate the different lines and then the fields. Like that:

    $lines = explode("\n", $_POST['textarea']);
    foreach($lines as $line) {
        $fields = explode(',', $line);
        $name = $fields[0];
        $email = $fields[1];

        // DO YOUR MYSQL STUFF HERE
    }

There might be better ways out there, but that would work for you.

<?php
$textareastring = $_POST['textareaname'];

$array = explode("\n",$textareastring );

foreach($array as $value) {
 $data = explode(',',$value);
 $name = $data[0];
 $email = $data[1];

//Your mysql query goes here
}
?>
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top