Jump to content


Photo

Parsing Text Files - Need help!


  • Please log in to reply
5 replies to this topic

#1 Maximus

Maximus
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 26 October 2006 - 05:52 AM

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:

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

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!

#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 26 October 2006 - 10:48 AM

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

name - agent smith
age - 24
hobby - computers and talking

name - User2
age - 98
hobby - something

name - Some_Other_User
age - 15
hobby - Writing


The following will output everything in a table:
<?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>";

?>


This was tested, and it worked :)

Orio.
Think you're smarty?

(Gone until 20 to November)

#3 Nicklas

Nicklas
  • Members
  • PipPipPip
  • Advanced Member
  • 111 posts
  • Locationwww.nswardh.com

Posted 27 October 2006 - 12:56 AM

Here´s another way
<?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);

?>


#4 Maximus

Maximus
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 27 October 2006 - 02:42 AM

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.

#5 Nicklas

Nicklas
  • Members
  • PipPipPip
  • Advanced Member
  • 111 posts
  • Locationwww.nswardh.com

Posted 27 October 2006 - 04:43 AM

Hmm, that´s weird, I get these results when I run my 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
        )

)


#6 Maximus

Maximus
  • Members
  • PipPip
  • Member
  • 14 posts

Posted 27 October 2006 - 09:28 PM

I think I have it figured out now, thanks a lot Nicklas :P

Also thanks to Orio for putting in the effort, thanks guys!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users