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
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.
Link to comment
Share on other sites

Here´s another way
[code]<?php

$file = file_get_contents('robots1.txt');
preg_match_all('/(?<=name - ).*?(?=\n)|(?<=age - ).*?(?=\n)|(?<=hobby - ).*?(?=\n|$)/', $file, $match);
$match = array_chunk($match[0], 3);

print_r($match);

?>[/code]
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.