Read TXT from PHP with paragraphs instead of lines

1

I have a little problem and I can not find it back.

Here surely someone can tell me where I face:

It turns out that I have a .txt (Flat Text File) with the following content:

laps=0
flag=Green
flagimg=C:\Users\marklubi\Desktop\AppBackgrounds\[email protected]

pos1fname=William
pos1lname=Prince
pos1num=48
pos1time=17.883
pos1laps=5
pos1blap=3
pos1btime=15.969
pos1diff=

pos2fname=Colby
pos2lname=Horner
pos2num=15
pos2time=17.980
pos2laps=5
pos2blap=3
pos2btime=15.968
pos2diff=0.229

pos3fname=Ethan
pos3lname=Endicott
pos3num=00
pos3time=17.054
pos3laps=4
pos3blap=3
pos3btime=16.062
pos3diff=1.429

leaderfname=WILLIAM
leaderlname=PRINCE
leadernum=48
leaderblap=3
leaderbtime=15.969

fastestfname=CAMERON
fastestlname=YOUNG
fastestnum=34
fastestblap=3
fastestbtime=15.864

I pretend to read it from PHP and then with the generate an xml, or show it, whatever.

I know how to do it with file () and then separate the fields. But how could it be done for this case, where they are not line by line, but let's say, in "paragraphs".

The first is unique. The following ones are repeated (between 1 and 40 can be) and the last two are also unique.

Does anyone have any idea how to approach this -?

Of course, I thank anyone who can provide an idea.

    
asked by Silvio Eduardo Stadelmann 25.05.2017 в 01:34
source

2 answers

0

You can open the file and iterate it line by line. Then by the name of the field you know that the line starts and the line ends (for the block), and you process it as you need it.

Example the first block starts in "pos". $ i. "fname" and closes in "pos". $ i. "diff".

Here I attach a code that can guide you

$ html = file_get_contents ($ url);         $ html = fix ($ html);

    $fileName = fileSave("bienes", $html);

    $file = fopen($fileName, "r");

    $htmlTable = "";
    $switch = 0;
    $tipo = 0;

    while (($line_detail = fgets($file)) !== false) {

        if (strpos($line_detail, "detalleSubasta.php") !== FALSE) {
            if (strpos($line_detail, 'Bienes') !== FALSE) {
                $tipo = 1;  
            }

            if (strpos($line_detail, 'Lotes') !== FALSE) {
                $tipo = 2;
            }
        }

        $pos = strpos($line_detail, 'datosSubastas');

        if ($pos !== FALSE) {
            /* Bien o Lote */
            $switch = 1;                                
        }

        if ($switch == 1) {
            $htmlTable .= $line_detail;
        }

        $pos = strpos($line_detail, '/table');
        if ($pos !== FALSE) {
            $switch = 0;                                
        }

    }

    $htmlTable = "<html>" . $htmlTable . "</html>";

Greetings,

    
answered by 25.05.2017 в 01:42
0

I found the beginning of the solution! Thanks Jorge Londoño!

I have filtered and eliminated row 1 (flags) and the last ones that are not positions. (Then I must find them again to add them together with the others outside the loop of the positions). Now I must add them to an xml, but it's easy and known.

So I leave the code:

<?php
$string = file_get_contents('data.txt');
$data  = preg_split("[\n\r]", $string);
foreach($data as $posicion=>$lineasdecampo)
{
if ($posicion == 0 || strpos($lineasdecampo, 'leader') || strpos($lineasdecampo, 'fastest')){ }else{        
    echo "<hr>Renglon " . $posicion ."<br>";
    $data  = preg_split("[\n\r]", $string);
    $datos = explode("\r", trim($lineasdecampo));
$mdatar = "";   
foreach($datos as $posicionx=>$datax)
    {
    $datofila = explode("=", $datax);   
    $mdatar .= $datofila[1].",";
    }   
echo $mdatar."<br>";
}   
}
?>

Greetings!

    
answered by 25.05.2017 в 20:13