tags:

views:

71

answers:

4

Sample ini file is

[SAMPLE.jpg]
faces=rect64(c18f4c8ef407851e),d4ff0a020be5c3c0;rect64(534a06d429ae627),dff6163dfd9d4e41;rect64(b9c100fae46b3046),e1059dcf6672a2b3;rect64(7b5105daac3a3cf4),4fc7332c107ffafc;rect64(42a036a27062a6c),ef86c3326c143248;rect64(31f4efe3bd68fd8),90158b3d3b65dc9b;rect64(327904e0614d390d),43cbda6e92fcb63e;rect64(4215507584ae9b8c),15b6a967e857f334;rect64(895d4efeb8b68425),5c4ff70ac70b27d3
backuphash=285
[Size.JPG]
faces=rect64(f73101cd554ca7f),43cbda6e92fcb63e
backuphash=38150
[ints.jpg]
faces=rect64(45c213047999593c),e1059dcf6672a2b3
backuphash=19801
[SC.jpg]
faces=rect64(993f2dfdab7f5166),e1059dcf6672a2b3;rect64(4b002f365a004c1b),ef86c3326c143248;rect64(bbffbb9fcb7fda25),ef86c3326c143248;rect64(bbbf9b10cb7fb996),90158b3d3b65dc9b;rect64(bbffdc97cb3ffa4c),4fc7332c107ffafc;rect64(5ec0306f734058b9),43cbda6e92fcb63e;rect64(65c06969827fa12b),15b6a967e857f334;rect64(bbff59f2cbbf7878),15b6a967e857f334;rect64(bbff7a81cb3f989f),43cbda6e92fcb63e
backuphash=9829
[karate.jpg]
faces=rect64(20823e7a6186b30b),15b6a967e857f334;rect64(92cb3e7ad34cb30b),15b6a967e857f334
backuphash=34154

Algorithm for pattern

[$name_of_picture]
faces=rect64($hex1_1),$hex1_2;rect64($hex2_1),hex2_2;....rect64($hexn_1),hexn_2;

I am interested in reading only the parts that are assigned by $var_name.. in the above code. How do I go about it?

Update

Using parse ini

<?php
//code from php.net

// Parse without sections
$ini_array = parse_ini_file("pic.ini");
print_r($ini_array);

// Parse with sections
$ini_array = parse_ini_file("pic.ini", true);
print_r($ini_array);

?>

Output

Warning: parse error in pic.ini on line 2 in C:\tezt\up.php on line 26

Warning: parse error in pic.ini on line 2 in C:\tezt\up.php on line 30

Update2

<?php

function new_parse_ini($f)
{

    // if cannot open file, return false
    if (!is_file($f))
        return false;

    $ini = file($f);

    // to hold the categories, and within them the entries
    $cats = array();

    foreach ($ini as $i) {
        if (@preg_match('/\[(.+)\]/', $i, $matches)) {
            $last = $matches[1];
        } elseif (@preg_match('/(.+)=(.+)/', $i, $matches)) {
            $cats[$last][$matches[1]] = $matches[2];
        }
    }

    return $cats;

}

?>

Output

Array ( [SAMPLE.jpg] => Array ( [faces] => rect64(c18f4c8ef407851e),d4ff0a020be5c3c0;rect64(534a06d429ae627),dff6163dfd9d4e41;rect64(b9c100fae46b3046),e1059dcf6672a2b3;rect64(7b5105daac3a3cf4),4fc7332c107ffafc;rect64(42a036a27062a6c),ef86c3326c143248;rect64(31f4efe3bd68fd8),90158b3d3b65dc9b;rect64(327904e0614d390d),43cbda6e92fcb63e;rect64(4215507584ae9b8c),15b6a967e857f334;rect64(895d4efeb8b68425),5c4ff70ac70b27d3 [backuphash] => 285 ) [Size.JPG] => Array ( [faces] => rect64(f73101cd554ca7f),43cbda6e92fcb63e [backuphash] => 38150 ) [ints.jpg] => Array ( [faces] => rect64(45c213047999593c),e1059dcf6672a2b3 [backuphash] => 19801 ) [SC.jpg] => Array ( [faces] => rect64(993f2dfdab7f5166),e1059dcf6672a2b3;rect64(4b002f365a004c1b),ef86c3326c143248;rect64(bbffbb9fcb7fda25),ef86c3326c143248;rect64(bbbf9b10cb7fb996),90158b3d3b65dc9b;rect64(bbffdc97cb3ffa4c),4fc7332c107ffafc;rect64(5ec0306f734058b9),43cbda6e92fcb63e;rect64(65c06969827fa12b),15b6a967e857f334;rect64(bbff59f2cbbf7878),15b6a967e857f334;rect64(bbff7a81cb3f989f),43cbda6e92fcb63e [backuphash] => 9829 ) [karate.jpg] => Array ( [faces] => rect64(20823e7a6186b30b),15b6a967e857f334;rect64(92cb3e7ad34cb30b),15b6a967e857f334 [backuphash] => 34154 ) )

So far so good. Thank you guys. This question is related to another question of mine http://stackoverflow.com/questions/3872112/automatic-face-detection-using-api

+7  A: 

PHP has a built-in function for parsing INI files. parse_ini_file()

Pekka
Beat me to it :-S Remember `$process_sections = true` (second arg.)
jensgram
Wrikken
@Wrikken Ah, I was just posting this as an answer. Hmmm, not my day :(
jensgram
I am getting a parse error in line number 2. while trying to parse the above file
abel
@abel you may need to wrap the values in quotes: `"faces=rect64(c18f4c......"`
Pekka
@Pekka i have updated the question with a working function which uses regex. do comment.
abel
@abel it shouldn't really be necessary. Is wrapping the values in quotes not an option?
Pekka
@Pekka I'd have to use regex to find faces and wrap it with quotes! BTW the other fn is working so no probs. Thx!
abel
@abel ah okay, if you can't change the input file the 2nd function will indeed work better.
Pekka
+2  A: 

Not everything related to strings is best answered with a regex.

In this case, you have functionality built in to PHP that does this for you.

http://php.net/manual/en/function.parse-ini-file.php

Andy Lester
@Andy Lester yeah I know http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags/1732454#1732454
abel
Actually, regexes make a lot of sense for parsing INI files. However, programmers seem blinded by the possibility of writing regexes which are never as good a choice as existing tested working code.
Andy Lester
+2  A: 

To elaborate on Pekka's answer:

  1. Parse file via $ini = parse_ini_file(<file>, true)
  2. Select faces=... by image name: $str = $ini[$name_of_picture]['faces']
  3. explode() on ;
  4. Iterate those and explode on ,

(You may want to make sure that the section ($name_of_picture) and directive (faces) exist, see isset().)

jensgram
+1 Informative.
abel
+1  A: 

In case you are curious how to parse it with regex, or maybe you are lazy to write relatively long code, here it is using regex:

^\s*\[([^\]]+)\]

You can refer to text inside the square-brackets using $1 in replace part, or \1 if you refer to in search part.

However, I agree you should use built-in PHP library for parsing INI files for serious projects.

Vantomex