How to change function explodes to import file . txt using colon

Asked

Viewed 67 times

0

How to do the function: explode(',', $linha); import a . txt file separated by two dots in a . txt file instead of using the comma.

The file . txt is currently this

Cesar, [email protected], cesar, 123456
Kelly, [email protected], kelly, 123457
Jessica, [email protected], jessica, 123458

The idea would be to stay that way

Cesar:[email protected]:cesar:123456
Kelly:[email protected]:kelly:123457
Jessica:[email protected]:jessica:123458

Php code

//Incluir a conexao com BD
include_once("conexao.php");

//Receber os dados do formulário
$arquivo_tmp = $_FILES['arquivo']['tmp_name'];

//ler todo o arquivo para um array
$dados = file($arquivo_tmp);

//Ler os dados do array
foreach($dados as $linha){
    //Retirar os espaços em branco no inicio e no final da string
    $linha = trim($linha);
    //Colocar em um array cada item separado pela virgula na string
    $valor = explode(',', $linha);

    //Recuperar o valor do array indicando qual posição do array requerido e atribuindo para um variável
    $nome = $valor[0];
    $email = $valor[1];
    $usuario = $valor[2];
    $senha = $valor[3];

    //Criar a QUERY com PHP para inserir os dados no banco de dados
    $result_usuario = "INSERT INTO usuarios (nome, email, usuario, senha) VALUES ('$nome', '$email', '$usuario', '$senha')";

    //Executar a QUERY para inserir os registros no banco de dados com MySQLi
    $resultado_usuario = mysqli_query($conn, $result_usuario);  
}
//Criar a variável global com a mensagem de sucesso
$_SESSION['msg'] = "<p style='color: green;'>Carregado os dados com sucesso!</p>";
//Redirecionar o usuário com PHP para a página index.php
header("Location: index.php");

By uploading the file. txt to the server separating the name, email and password data with two dots and no spaces all information is only entered in the database name field. inserir a descrição da imagem aqui

  • Would you accept a response that uses only the shell command? (Not using php). My suggestion is to use sed.

  • 1

    Barter explode(',', $linha); for explode(':', $linha);.

  • @Benilson when changing to two points he imports everything in one column in the database as shown in the image of phpmyadmin.

  • 1

    The file is with :or ,? Not clear. You want to change the txt file with , for : or he’s already like this?

  • @Tiagoa has two points. The problem is that with two points it imports all the data for only one column, when it should separate by name, email, user and password.

  • I did a test here online and it worked with your code: https://repl.it/@tiagoa/txt-two-points

  • @Tiagoa It really worked, thank you.

  • the ideal would be to make a single INSERT outside the FOREACH.

  • instead of creating the QUERY within the loop create the Insert VALUES

  • $dataString .= " ('$name', '$email', '$user', '$password'),";

  • out of the loop remove the last comma $values=substr($dataString, 0, -1);

  • and do the Insert like this: $result_usuario = "INSERT INTO usuarios (name, email, user, password) VALUES $values";

  • as stated earlier, make the Insert out of the loop

  • @Leocaracciolo Thanks for the tips, I will add in the code.

Show 9 more comments
No answers

Browser other questions tagged

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