Maximus Posted October 26, 2006 Share Posted October 26, 2006 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 smithage - 24hobby - 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 More sharing options...
Orio Posted October 26, 2006 Share Posted October 26, 2006 Ok, so let's say we have a file called data.txt that contains something like this:[code]name - agent smithage - 24hobby - computers and talkingname - User2age - 98hobby - somethingname - Some_Other_Userage - 15hobby - 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 https://forums.phpfreaks.com/topic/25139-parsing-text-files-need-help/#findComment-114672 Share on other sites More sharing options...
Nicklas Posted October 27, 2006 Share Posted October 27, 2006 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 https://forums.phpfreaks.com/topic/25139-parsing-text-files-need-help/#findComment-115170 Share on other sites More sharing options...
Maximus Posted October 27, 2006 Author Share Posted October 27, 2006 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 https://forums.phpfreaks.com/topic/25139-parsing-text-files-need-help/#findComment-115201 Share on other sites More sharing options...
Nicklas Posted October 27, 2006 Share Posted October 27, 2006 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 https://forums.phpfreaks.com/topic/25139-parsing-text-files-need-help/#findComment-115230 Share on other sites More sharing options...
Maximus Posted October 27, 2006 Author Share Posted October 27, 2006 I think I have it figured out now, thanks a lot Nicklas :PAlso thanks to Orio for putting in the effort, thanks guys! Link to comment https://forums.phpfreaks.com/topic/25139-parsing-text-files-need-help/#findComment-115588 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.