- 论坛徽章:
- 0
|
<?php
class ini_parser
{
// $last_sect : used to special the last parsed section name.
private $last_sect;
function parse (&$path, &$store)
{
if (file_exists($path)) {
$read = @file($path);
foreach ($read as $value) {
if (!$this->is_comment ($value)) {
if ($this->is_section($value)) {
// parse such section name.
$value = trim($value, "[");
$value = str_replace ("]", "", $value);
if (!$this->section_exist ($value, $store)) {
$this->last_sect = $value;
}
} else {
// parse contents.
$exp = explode("=", $value);
$store[$this->last_sect][$exp[0]] = $exp[1];
}
}
}
}
}
private function is_comment (&$str)
{
if (isset($str))
{
return (';' == $str[0] || '#' == $str[0]);
}
return false;
}
private function is_section (&$str)
{
$check = trim($str);
return ($str[0] == '[' && $check[strlen ($check) - 1] == ']');
}
private function section_exist (&$sect, &$coll)
{
if (is_array(&$coll))
{
return array_key_exists($sect, $coll);
}
return false;
}
}
?>
接触php不久,其中有不妥的地方还请指教。不知该类在实用时怎样。
交各php爱好者:
QQ:9753114 |
|