Jump to content

Parsing Text Files - Need help!


Maximus

Recommended Posts

Hi, I have been trying to parse a text file and then retrieve information from it.  Sadly though, I have tried various methods and gotten nothing to work the way I wanted it to work.  I want to parse a .txt file and then retrieve 3 things from it: "name", "age", "hobby" and store each in its own variable..  My text file looks like this:

[code]name - nancy smith
age - 24
hobby - painting, playing computer games, reading and horse-back riding!! [/code]

So basically the code needs to search for (example) "name" and parse until it finds the "enter" to the next line, at which point it stops and goes to "age". I don't know if this makes sense, but all I want it to do it retrieve each attribute and put it into a variable.  If someone could help me, that would be great!
Link to comment
https://forums.phpfreaks.com/topic/25139-parsing-text-files-need-help/
Share on other sites

Ok, so let's say we have a file called data.txt that contains something like this:

[code]name - agent smith
age - 24
hobby - computers and talking

name - User2
age - 98
hobby - something

name - Some_Other_User
age - 15
hobby - Writing[/code]


The following will output everything in a table:
[code]<?php


// Pull information
$data=file_get_contents("data.txt");
$data.="\n"; //So the regex will also match the last record.


// Match data
$regex="/name\s*-\s*(.*?)\s*age\s*-\s*([0-9]+)\s*hobby\s*-\s*(.*?)\n/";
preg_match_all($regex,$data,$matches);



// Some HTML...
echo '<table border="1"><tr><td width="20%">Name</td><td width="10%">Age</td><td>Hobbies</td></tr>';


// Run loop to display information
$max=count($matches[1])-1;
$i=0;

while($i<=$max)
{
echo "<tr>";
echo "<td>".$matches[1][$i]."</td>";
echo "<td>".$matches[2][$i]."</td>";
echo "<td>".$matches[3][$i]."</td>";
echo "</tr>";

$i++;
}

echo "</table>";

?>[/code]


This was tested, and it worked :)

Orio.
I tried your method Nicklas, but it puts it all into one big string when printed.

I need it seperate...age into $age, hobby into $hobby etc.

I also tried Orio's code, and while I found that to work, it has the same problem, I need each "answer" (text behind the -) to be put into a seperate variable that I can use later on. Thanks.
Hmm, that´s weird, I get these results when I run my code:
[code]Array
(
    [0] => Array
        (
            [0] => agent smith
            [1] => 24
            [2] => computers and talking
        )

    [1] => Array
        (
            [0] => User2
            [1] => 98
            [2] => something
        )

    [2] => Array
        (
            [0] => Some_Other_User
            [1] => 15
            [2] => Writing
        )

)[/code]

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.