Explode lines from a textarea and separate them by commas

Asked

Viewed 130 times

0

Opa,

I need to capture the text of the textarea, separate it by lines and then explode and separate it by commas.

        $lines = explode("\n", $respostas);
        $i = 0;
        if ( !empty($lines) )
        {
            foreach ($lines as $line)
            {
                $i++;
                    //Adiciona Respostas
                    foreach($respostas as $key)
                    {   
                        $sql = mysqli_query($conn, "INSERT INTO respostas
                            VALUES (
                                NULL, $i, '$key'
                        )");
                    }
                    //Adiciona Respostas

            }
        }

Where in mysql Insert, I need to save the content ($key) to the line ($i), but, is duplicating the Inserts according to the number of lines.

Example of the textarea:

teste, teste1, teste2
teste3, teste4
  • What’s in the $answers?

  • Contents of the textarea: 
teste, teste1, teste2
teste3, teste4
 Words must be separated by comma and line

  • Got it, it comes this way then

  • In the @Andrébaill case, here does not display formatting, I added the textarea’s sample content to the question

  • tell me one thing, will all be separated by comma already? and another thing... the goal is to insert these records into the database?

  • Yes, the user will already enter the words separating them with the comma, including the separation between lines and will be for registration in the bank

Show 2 more comments

1 answer

2


I made it here and I imagine that’s what you want, see if it meets:

$respostas = "teste, teste1, teste2
        teste3, teste4";
        $lines = explode("\n", $respostas);
        $i = 0;
        if ( !empty($lines) )
        {
            $aWords = [];
            foreach($lines as $line){
                $aWords[] = explode(',', $line);
            }
            //Adiciona Respostas
            foreach($aWords as $key => $aWord2)
            {   
                $linha = ($key+1);
                foreach($aWord2 as $word){
                    $word = trim($word);
                    $query = "INSERT INTO respostas VALUES (NULL, $linha, '{$word}')\n";
                    echo nl2br($query);
                    //$sql = mysqli_query($conn, $query);
                }
            }
        }

Result was thus:

INSERT INTO respostas VALUES (NULL, 1, 'teste')
INSERT INTO respostas VALUES (NULL, 1, 'teste1')
INSERT INTO respostas VALUES (NULL, 1, 'teste2')
INSERT INTO respostas VALUES (NULL, 2, 'teste3')
INSERT INTO respostas VALUES (NULL, 2, 'teste4')

Uncomment the $sql line to insert into the base or change as extra needs.

Good luck!

  • Good afternoon Fabil, your script worked perfectly, but when upar to the online server, is returning me Parse error: syntax error, unexpected '[' , that’s where the line is $aWords = [];. Is there anything that can be done to get around? Hug

  • replace the [] keys with: array(); leaving: $aWords = array();

Browser other questions tagged

You are not signed in. Login or sign up in order to post.