PHP read/sort

Soldato
Joined
24 Nov 2002
Posts
16,378
Location
38.744281°N 104.846806°W
I have the folllowing type of data in a 10mb flat file database:

Code:
[Term]
id: 1
name: blah
namespace: a
def: blah

[Term]
id: 2
name: blah2
namespace: b
def: blah2

[Term]
id: 3
name: blah3
namespace: c
def: blah3

[Term]
id: 4
name: blah4
namespace: a
def: blah4

... so on....

Note that each 'set' of these may have any number of fields but all start with [Term] and have a unique ID on the next line.

I want to, in php preferably, be able to load up the text file and 'sort' the contents into three seperate files based upon the value of the *namespace* field (i.e. a, b and c). This sounds simple enough but I'm stumped.

The methods I've used before rely on the fields all being on one line and having no headings so I'm mightily confused!

Any pointers?
 
Thanks. I'm reading through the page and examples now.

I already have:

Code:
<?php
$lines = file('file.file');
foreach ($lines as $line) {
   echo "". $line . "<br />\n";
}
?>

To load and display the data (all of it!). The problem I've encountered is the fact the fields are on new lines and with headers.
 
Bacially, what I want to do is:

A.
1) Define group 'start' as [term]
2) Define group 'end' as \n.
3) Read groups.
4) If line "is_obsolete: true" exists in a group, remove group from file.

B.
1) Define group 'start' as [term]
2) Define group 'end' as \n.
3) Read groups.
4) If group contains line "namespace: a", copy group to file a.
else
5) If group contains line "namespace: b", copy group to file b.
else
6) If group contains line "namespace: c", copy group to file c.

I'm guessing part of this involves "eregi"?
 
Last edited:
Dj_Jestar said:
Multiple [Term] will overwrite each other for inifile.
lol... I just found that out using the following from php.net:

Code:
<?php
// Looks for [] characters to mark section headings and : chars to mark the break between the key and its values.

function parse_ini_file_quotes_safe($f)
{
 $newline = "/n";
 $null = "";
 $r=$null;
 $first_char = "";
 $sec=$null;
 $header_section = "";

 //Read to end of file with the newlines still attached into $f
 $f=@file($f);
 // Process all lines from 0 to count($f) 
 for ($i=0;$i<@count($f);$i++)
 {
  $newsec=0;
  $w=@trim($f[$i]);
  $first_char = @substr($w,0,1);
  if ($w)
  {
   if ((!$r) or ($sec))
   {
   // Look for [] chars round section headings
   if ((@substr($w,0,1)=="[") and (@substr($w,-1,1))=="]") {$sec=@substr($w,1,@strlen($w)-2);$newsec=1;}
   //
   }
   if (!$newsec)
   {
   //
   // Look for the = char to allow us to split the section into key and value 
   $w=@explode(":",$w);$k=@trim($w[0]);unset($w[0]); $v=@trim(@implode("=",$w));
   // look for the new lines 
   if ((@substr($v,0,1)=="\"") and (@substr($v,-1,1)=="\"")) {$v=@substr($v,1,@strlen($v)-2);}
   if ($sec) {$r[$sec][$k]=$v;} else {$r[$k]=$v;}
   }
  }
 }
 return $r;
}

print_r(parse_ini_file_quotes_safe("file.file"));

?>
 
robmiller said:
Here's one that parses out the values and sorts by the namespace value:

Edit: vB is putting some whitespace in there that shouldn't be there. Try this:

http://pwnt.co.uk/stuff/jd.phps
Once again Rob, you're a star.... I think I may have to start paying you... one day :D

Quick question. For some bizzare reason, each 'group' of data has different number of fields, e.g. group 1 may have abcdefg but group 2 abcfgh. If I define all the fields at the start of your script, it won't matter if they're 'missing' will it?

If that didn't make sense, I'll try and re-explain.

edit - OR, how can I modify it such that the output only contains the three fields ID, name and namespace, and disregard all the other fields. Would I just not include them in the opening?
 
Last edited:
Back
Top Bottom