Actions

API16

JRegistryFormatINI/stringToObject

From Joomla! Documentation

< API16:JRegistryFormatINI
Revision as of 17:54, 22 March 2010 by Doxiki (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Replacement filing cabinet.png
This Namespace has been archived - Please Do Not Edit or Create Pages in this namespace. Pages contain information for a Joomla! version which is no longer supported. It exists only as a historical reference, will not be improved and its content may be incomplete.

Contents

Description

Parse an .ini string, based on phpDocumentor phpDocumentor_parse_ini_file function

[Edit Descripton]

Description:JRegistryFormatINI/stringToObject

Syntax

stringToObject($data, $process_sections=false)
Parameter Name Default Value Description
$data The INI string or array of lines
$process_sections false Add an associative index for each section [in brackets]

Returns

object Data Object

Defined in

libraries/joomla/registry/format/ini.php

Importing

jimport( 'joomla.registry.format.ini' );

Source Body

public function stringToObject($data, $process_sections = false)
{
        static $inistocache;
 
        if (!isset($inistocache)) {
                $inistocache = array();
        }
 
        if (is_string($data)) {
                $lines = explode("\n", $data);
                $hash = md5(intval($process_sections).$data);
        } else {
                if (is_array($data)) {
                        $lines = $data;
                } else {
                        $lines = array ();
                }
                $hash = md5(intval($process_sections).implode("\n",$lines));
        }
 
        if (array_key_exists($hash, $inistocache)) {
                return $inistocache[$hash];
        }
 
        $obj = new stdClass();
 
        $sec_name = '';
        $unparsed = 0;
        if (!$lines) {
                return $obj;
        }
 
        foreach ($lines as $line) {
                // ignore comments
                if ($line && $line{0} == ';') {
                        continue;
                }
 
                $line = trim($line);
 
                if ($line == '') {
                        continue;
                }
 
                $lineLen = strlen($line);
                if ($line && $line{0} == '[' && $line{$lineLen-1} == ']') {
                        $sec_name = substr($line, 1, $lineLen - 2);
                        if ($process_sections) {
                                $obj-> $sec_name = new stdClass();
                        }
                } else {
                        if ($pos = strpos($line, '=')) {
                                $property = trim(substr($line, 0, $pos));
 
                                // property is assumed to be ascii
                                if ($property && $property{0} == '"') {
                                        $propLen = strlen($property);
                                        if ($property{$propLen-1} == '"') {
                                                $property = stripcslashes(substr($property, 1, $propLen - 2));
                                        }
                                }
                                $value = substr($line, $pos +1);
 
                                if (strpos($value, '|') !== false && preg_match('#(?<!\\\)\|#', $value)) {
                                        $newlines = explode('\n', $value);
                                        $values = array();
 
                                        foreach($newlines as $newlinekey=>$newline) {
                                                // Explode the value if it is serialized as an arry of value1|value2|value3
                                                $parts  = preg_split('/(?<!\\\)\|/', $newline);
                                                $array  = (strcmp($parts[0], $newline) === 0) ? false : true;
                                                $parts  = str_replace('\|', '|', $parts);
 
                                                foreach ($parts as $key => $value) {
                                                        if ($value == 'false') {
                                                                $value = false;
                                                        } else if ($value == 'true') {
                                                                $value = true;
                                                        } else if ($value && $value{0} == '"') {
                                                                $valueLen = strlen($value);
                                                                if ($value{$valueLen-1} == '"') {
                                                                        $value = stripcslashes(substr($value, 1, $valueLen - 2));
                                                                }
                                                        }
                                                        if (!isset($values[$newlinekey])) {
                                                                $values[$newlinekey] = array();
                                                        }
                                                        $values[$newlinekey][] = str_replace('\n', "\n", $value);
                                                }
 
                                                if (!$array) {
                                                        $values[$newlinekey] = $values[$newlinekey][0];
                                                }
                                        }
 
                                        if ($process_sections) {
                                                if ($sec_name != '') {
                                                        $obj->$sec_name->$property = $values[$newlinekey];
                                                } else {
                                                        $obj->$property = $values[$newlinekey];
                                                }
                                        } else {
                                                $obj->$property = $values[$newlinekey];
                                        }
                                } else {
                                        //unescape the \|
                                        $value = str_replace('\|', '|', $value);
 
                                        if ($value == 'false') {
                                                $value = false;
                                        } else if ($value == 'true') {
                                                $value = true;
                                        } else if ($value && $value{0} == '"') {
                                                $valueLen = strlen($value);
                                                if ($value{$valueLen-1} == '"') {
                                                        $value = stripcslashes(substr($value, 1, $valueLen - 2));
                                                }
                                        }
 
                                        if ($process_sections) {
                                                $value = str_replace('\n', "\n", $value);
                                                if ($sec_name != '') {
                                                        $obj->$sec_name->$property = $value;
                                                } else {
                                                        $obj->$property = $value;
                                                }
                                        } else {
                                                $obj->$property = str_replace('\n', "\n", $value);
                                        }
                                }
                        } else {
                                if ($line && $line{0} == ';') {
                                        continue;
                                }
 
                                if ($process_sections) {
                                        $property = '__invalid'.$unparsed ++.'__';
                                        if ($process_sections) {
                                                if ($sec_name != '') {
                                                        $obj->$sec_name->$property = trim($line);
                                                } else {
                                                        $obj->$property = trim($line);
                                                }
                                        } else {
                                                $obj->$property = trim($line);
                                        }
                                }
                        }
                }
        }
 
        $inistocache[$hash] = clone $obj;
        return $obj;
}

[Edit See Also] SeeAlso:JRegistryFormatINI/stringToObject

Examples

<CodeExamplesForm />