On Tue, 15 May 2001 02:19:52 -0400, you wrote:

>>[StoreData]
>>Name=Ben Hurr
>
>Has anyone a few lines of code that could help read a couple of variables in an
>*.ini file?

Here's something I did in a hurry. It can be greatly improved, but it
appears to work. The class is called IniReader. I put it into an include
file called inireader.inc.php, so that's why the example shows that as an
include line. The constructor takes 3 parameters - the file name, the
section heading, and the variable desired. See the example which returns
"United States".

First the example code (real short):

=================================

<?php
        include('inireader.inc.php');

        $ini = new IniReader('c:\windows\win.ini', '[intl]', 'scountry');
        $ini->findvalue();
        if ($ini->errorval == 0)
        {
                if (empty($ini->requiredvalue))
                        print("Not found or blank.<BR>");
                else
                        print($ini->requiredvalue."<BR>");
        }
        else
                print("Error #".$ini->errorval." encountered.<BR>");
?>

=================================

Now the class itself - (bigger, but not huge): it should be improved with
better error handling, but as a first pass.... what the heck:
Read it carefully, because word-wrap (yours or mine or both) is probably
going to mess it up pretty good.

=================================

<?php

class IniReader
{
        var $filename;
        var $sectionkey;
        var $varkey;
        var $errorval;
        var $linearray;
        var $requiredvalue;

//      constructor takes name of ini file (with path) - ie
"c:\windows\win.ini",
//      section - ie "[intl]",
// variable to resolve - ie "sCountry"
// with above, on my system, should return "United States"

        function IniReader($filename = "", $sectionkey = "", $varkey = "")
        {
                $this->filename = $filename;
                $this->sectionkey = $sectionkey;
                $this->varkey = $varkey;
                $this->errorval = 0;
                $this->requiredvalue = "";
                $this->linearray = array("");
        }

        // function to pull ini file into an array
        function getfilearray()
        {
                if (empty($this->filename))
                        $this->errorval = 1;            // no filename provided
                elseif (!is_file($this->filename))
                        $this->errorval = 2;            // no such file (or invalid 
filename)
                else
                        $this->linearray = file($this->filename);
        }

        function findvalue()
        {
                $this->getfilearray();          // load up array with ini file

                if ($this->errorval == 0)               // seems ok
                {
                        if (empty($this->sectionkey))
                                $this->errorval = 3;            // no sectionkey 
provided
                        else
                        {
                                $eleval = current($this->linearray);            // get 
1st line of file
(array element 0)

                                // watch for end of file and look for sectionkey 
requested
                                while (!($eleval === false) and
                                                (strncasecmp($eleval, 
$this->sectionkey,
strlen($this->sectionkey)) != 0))
                                        $eleval = next($this->linearray);              
 // not found, so continue

                                $eleval = next($this->linearray);               // 
move to next line (could be
eof)

                                // watch for eof and only go until next section header 
encountered
                                // if next section header found, then requested 
variable value not
in this section,
                                // so don't bother continuing
                                while (!($eleval === false) and 
(strcmp(substr($eleval, 0, 1), "[")
!= 0))
                                {
                                        // did we find the variable desired?
                                        if (strncasecmp($eleval, $this->varkey, 
strlen($this->varkey)) ==
0)
                                        {
                                                // yes, so put it into 
this->requiredvalue and quit looking
                                                $tmpval = stristr($eleval, "=");

                                                if (!($tmpval === false))   // no 
value at all??? ok, possible
                                                        $this->requiredvalue = 
trim(substr($tmpval, 1, strlen($tmpval)
- 1));

                                                break;
                                        }
                                        else            // not found yet so continue
                                                $eleval = next($this->linearray);
                                }
                        }
                }
        }
}

?>

==============================================


>Thanks,
>
>John
>
>Alan Popow wrote:
>
>> On Mon, 14 May 2001 21:48:54 -0400, you wrote:
>>
>> Ahh.. Ok. I see what you're getting at.
>>
>> While I haven't tried it, it probably wouldn't be a lot of code to create a
>> function to do it using file() to read the .ini file into an array and then
>> using array_search() to find the required information.
>>
>> Alan
>>
>> >There is no procedure in place, no one has created a module to ... to read ...
>> >say ...
>> >
>> >[StoreData]
>> >Name=Ben Hurr
>> >
>> >Not that I'm going to go Perling just for this, but ...
>> >
>> >??
>> >John
>> >
>> >Alan Popow a écrit :
>> >
>> >> On Mon, 14 May 2001 18:30:48 -0400, you wrote:
>> >>
>> >> A *.ini file is just a common garden variety text file. Read it however you
>> >> read any other text file.
>> >>
>> >> Alan
>> >>
>> >> >How can I read the contents of an *.ini file?
>> >> >Is there an example kicking around that I did not see?
>> >> >
>> >> >Thanks,
>> >> >
>> >> >John
>> >> >
>> >> >--
>> >> >John Taylor-Johnston
>> >> >Langues Modernes
>> >> >poste 289
>> >>
>> >> --
>> >> PHP Windows Mailing List (http://www.php.net/)
>> >> To unsubscribe, e-mail: [EMAIL PROTECTED]
>> >> For additional commands, e-mail: [EMAIL PROTECTED]
>> >> To contact the list administrators, e-mail: [EMAIL PROTECTED]
>> >
>> >--
>> >John Taylor-Johnston
>> >Langues Modernes
>> >poste 289
>>
>> --
>> PHP Windows Mailing List (http://www.php.net/)
>> To unsubscribe, e-mail: [EMAIL PROTECTED]
>> For additional commands, e-mail: [EMAIL PROTECTED]
>> To contact the list administrators, e-mail: [EMAIL PROTECTED]


--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to