How to read and block this TXT with PHP?

Asked

Viewed 146 times

0

How can I read a TXT file in this format to insert into an episode table? This is the structure:

temporada{
    numero_episodio - nome_do_episodio
}

A practical example:

1{
    1 - Episódio número 1 na 1ª Temporada
    2 - Episódio número 2 na 1ª Temporada
    ...
}
2{
    1 - Episódio número 1 na 2ª Temporada
    2 - Episódio número 2 na 2ª Temporada
    ...
}

How to traverse, read and separate these lines so that an array is in the following format: episodios[temporada][episodio][nome_do_episodio], is it possible? Is there any easier way to organize this TXT?

  • I don’t have much experience with preg_split and preg_match functions, can give me a practical example of how to apply them to my example?

  • 3

    I had a doubt Leo, this format you invented? Have you heard of JSON? I don’t really know if it pays to create a parse of your own when you can use a more used format ;)

  • Sometimes it will be necessary to add a large amount of episodes at once, this was the easiest format I found for other users to add more lines at a time, I will even create a form for such, but I want to make this option available as well, but I will check the possibility to use json

  • 5

    Leaving such an operation for the user to modify manually is a huge risk. As simple and intuitive as it makes the format the user will manage to spoil most of the time.

  • It’s just someone other than me, it would be useful because many times we need to leave the episodes ready to be inserted, but we don’t want them in the database yet, so it would be useful to have them separated into a Txt, but I will leave only a form with dynamic fields.

  • Have you ever thought to insert them into the system and simply leave as active or inactive?

  • 1

    I’ll think it over, and I’ll see with the other person if he really wants a way so much simpler that he can’t use a form for such

Show 2 more comments

1 answer

2


If it is only two people who will edit the DOC has nothing better than to use a more "exact" format like JSON or XML, instead of inventing its own format, anyway a +or- simple way is with regular expressions and replace, took me a while but I managed to create an example:

<?php

function converteParaJson($str) {
    //Primeiro iremos converter fazer um parser para JSON +ou- assim:

    //Cria as temporadas
    $str = preg_replace('#^(|\s)+(\d+)(\s|)\{#', '"$2": {', $str);
    $str = preg_replace('#\}(\s|)+(\d+)\{#', '}, "$2": {', $str);

    //Cria os episódios
    $str = preg_replace('#(\d+)(\s+|)\-(\s+|)([^\r\n]+)#', '"$1": "$4",', $str);

    //Remove virgulas extras
    $str = preg_replace('#,[\s]+\}#', '}', $str);

    $str = '{' . $str . '}';

    return $str;
}

$txt = '1{
    1 - Episódio número 1 na 1ª Temporada
    2 - Episódio número 2 na 1ª Temporada
}
2{
    1 - Episódio número 1 na 2ª Temporada
    2 - Episódio número 2 na 2ª Temporada
}';

$txt = converteParaJson($txt);

//Converte o json para array do php
$data = json_decode($txt, true);

print_r($data);

See it working on ideone: https://ideone.com/SEwPqz

  • Will it work even if you have more than two seasons? Thanks for the example William, I will take as an example to study the preg_replace better

  • 1

    @Leoletto does work, so it has 2 preg_replacefor the seasons, where I wrote //Cria as temporadas, one is for first season and another for the others regardless of the amount. If something fails let me know

Browser other questions tagged

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