How to separate the data that comes from a TextArea in PHP?

0

Good afternoon what I want to do is get all the data that is entered in a textarea and in php to be able to separate them by lina jump and by coma

Like this:

I'm using codeigniter

$variablecontenedora = $this->input->post("contenedor");

the three rows are stored in the variable $ variablecontener.

Familia 01 , Grupo 01 , Descripcion 01
Familia 02 , Grupo 02 , Descripcion 02
Familia 03 , Grupo 03 , Descripcion 03

and I want to spread it this way

the first row:

$fila_1 = Familia 01 , Grupo 01 , Descripcion 01;

then the data of that variable I want to separate them by the commas

$coma_1:Familia 01;
$coma_2:Grupo 01;
$coma_3:Descripcion 01;

and so on with the other rows that are in the texarea

The number of columns is exact, only 3 will be.

The rows are indeterminate

    
asked by Luis 19.07.2018 в 23:38
source

2 answers

1

you can apply the explode function twice. In the first function, it generates an element for each textarea line. And then apply the explode to each line again to separate it by commas. The result is stored in a multi-dimensional array $ separated_by_comas, where each element of the array is in turn another array with three elements, one for each column.

$separado_por_salto = explode("\n", $variablecontenedora);

foreach($separado_por_salto as $lineas){
    $separado_por_comas[] = explode(",", $lineas);
};

I hope it serves you.

    
answered by 19.07.2018 / 23:55
source
1

You can use explode , which separates strings using the character you want.  First you can separate by line change \n and then by commas like this:

$filas = explode("\n", $variablecontenedora);
foreach($filas as $fila){
  $comas = explode("," $fila);
  //Si quieres tres variables
  $coma_1 = $comas[0];
  $coma_2 = $comas[1];
  $coma_3 = $comas[2];
}
    
answered by 19.07.2018 в 23:44