Functions: $ini and $readini for PHP -> Updated + Updated Description

By ProIcons on Nov 13, 2012

$ini and $readini functions for PHP

Usage:
ini("file",N)
Returns the name/Nth position of the specified topic/item in an ini/text file.
The item/N parameter is optional. If you specify N = 0, it returns the total number of topics/items.

readini("file","section","item")
Returns a single line of text from an ini file

Example:
test.ini

[FirstRow]
steamid=Yak

[SecondRow]
steamid=Yik

[ThirdRow]
steamid=Yadjd

[FourthRow]
steamid=cols

    function echor($text=NULL) {
        echo $text."<br />";
    }
    $file="test.ini";
    echor("Sections in file: ".ini($file,0));
    echor("");
    for ($i=1;$i<=ini($file,0);$i++) {
        echor("Section ($i): ".ini($file,$i));
    }
    echor();
    for ($i=1;$i<=ini($file,0);$i++) {
        echor("Section ($i): ".ini($file,$i));
        echor("-Options: ".ini($file,ini($file,$i),0));
        for ($z=1;$z<=ini($file,ini($file,$i),0);$z++) {
            echor("--Option ($z): ".ini($file,ini($file,$i),$z)." Value ($z): ".readini($file,ini($file,$i),ini($file,ini($file,$i),$z)));
        }
        echor();
    }

Result

Sections in file: 4

Section (1): FirstRow
Section (2): SecondRow
Section (3): ThirdRow
Section (4): FourthRow

Section (1): FirstRow
-Options: 1
--Option (1): steamid Value (1): Yak

Section (2): SecondRow
-Options: 1
--Option (1): steamid Value (1): Yik

Section (3): ThirdRow
-Options: 1
--Option (1): steamid Value (1): Yadjd

Section (4): FourthRow
-Options: 1
--Option (1): steamid Value (1): cols

<?php
    function ini($file,$n,$n2 = NULL){
        $counter2=0;$counter=0;
        $handle = @fopen($file, "r"); 
        $contents = @fread($handle, filesize($file));
        if (!$handle || !$contents) return false; 
        $contents = split("\n", trim($contents)); 
        foreach ($contents as $k => $v) {
            if ((substr(trim($v), 0, 1) != ";") && (substr(trim($v), 0, 1) != "#")) {
                if (substr(trim($v), 0, 1) == "[") {
                    $counter++;
                    $head=substr(trim($v), 1, -1);
                    if (is_numeric($n) && !$n2) {
                        if ($counter == $n && $n != 0) {
                            return $head; 
                        }
                    }
                    else {
                        if ($head == $n && ($n2 == NULL && $n2<>0)) {
                            return $counter;
                        }
                    }
                } 
                elseif ((trim($v) <> "") and (substr($v, 0, 1) <> "#") and ($head == $n)) {
                    if ($n2 >= 0 && !is_numeric($n)) {
                        $counter2++;
                        $tmp = explode("=", $v);
                        if ($counter2 == $n2) {
                            return $tmp[0];
                        }
                    }
                }
            }
        }
        if ($n2<=0 && !is_numeric($n)) { return $counter2; }
        if ($n == 0 && !$n2) {
            return $counter;
        }
    }
    function readini($file,$section,$value) {
        $counter;
        $head;
        $handle = @fopen($file, "r"); 
        $contents = @fread($handle, filesize($file)); 
        if (!$handle || !$contents) return false;
        $contents = split("\n", trim($contents)); 
        foreach ($contents as $k => $v) {
            if ((substr(trim($v), 0, 1) != ";") && (substr(trim($v), 0, 1) != "#")) {
                if (substr(trim($v), 0, 1) == "[") {
                    $counter++;
                    $head=substr(trim($v), 1, -1);
                } 
                elseif ((trim($v) <> "") and (substr($v, 0, 1) <> "#")) { 
                    $tmp = explode("=", $v);
                    if ($value==trim($tmp[0]) && $section == $head) {
                        return trim($tmp[1]);
                    }
                } 
            }
        }
        @fclose($handle); 
    }

?>

Comments

Sign in to comment.
ProIcons   -  Nov 28, 2013

@Hawkee do you believe there is a way to do exactly this think with smaller code? Are there any tweeks can be made to this code? What's your opinion?\

Hawkee  -  Nov 28, 2013

Looks like your'e just doing a linear search which should be fine for your purposes. I'm sure there are more advanced methods, but this should suffice.

ProIcons  -  Nov 28, 2013

Don't get it. Please enlighten me what other advanced methods exists?

Hawkee  -  Nov 28, 2013

Well for example you could load the file into memory as a searchable hash which would allow for multiple queries with only a single scan of the file, but I don't think this would be necessary.

ProIcons  -  Dec 05, 2013

Think about an Active Listening Server with PHP, And in the other side a Web PHP Page. Now Lets say the PHP Page is our Settings Page, and the other part is our Server. Modifying the INI File through our Web Page would change our ini file. Though if we had loaded the whole ini file into our memory through our server, it wouldn't gonna read the new values. It would be with an out of date config.

I get it though, in some other apps, would be more useful to save it in an array , in order to make unlimited queries with less processing and reading time. This particular script can be modified to allow caching the data to a permanent array. When i find some free time, i will give it a try.

ProIcons  -  Sep 13, 2014

My Mistake, Misunderstood your hint.

Sign in to comment

Are you sure you want to unfollow this person?
Are you sure you want to delete this?
Click "Unsubscribe" to stop receiving notices pertaining to this post.
Click "Subscribe" to resume notices pertaining to this post.