Actions

API15

JRegistryFormatINI/objectToString

From Joomla! Documentation

< API15:JRegistryFormatINI
Revision as of 13:44, 12 May 2013 by JoomlaWikiBot (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

Converts an object into an INI formatted string Unfortunately, there is no way to have ini values nested further than two levels deep. Therefore we will only go through the first two levels of the object.


[<! removed edit link to red link >]

<! removed transcluded page call, red link never existed >

Syntax

objectToString(&$object, $params)
Parameter Name Default Value Description
&$object $object Data Source Object
$params $param Parameters used by the formatter

Returns

string INI Formatted String

Defined in

libraries/joomla/registry/format/ini.php

Importing

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

Source Body

function objectToString( &$object, $params )
{
 
        // Initialize variables
        $retval = '';
        $prepend = '';
 
        // First handle groups (or first level key/value pairs)
        foreach (get_object_vars( $object ) as $key => $level1)
        {
                if (is_object($level1))
                {
                        // This field is an object, so we treat it as a section
                        $retval .= "[".$key."]\n";
                        foreach (get_object_vars($level1) as $key => $level2)
                        {
                                if (!is_object($level2) && !is_array($level2))
                                {
                                        // Join lines
                                        $level2         = str_replace('|', '\|', $level2);
                                        $level2         = str_replace(array("\r\n", "\n"), '\\n', $level2);
                                        $retval         .= $key."=".$level2."\n";
                                }
                        }
                        $retval .= "\n";
                }
                elseif (is_array($level1))
                {
                        foreach ($level1 as $k1 => $v1)
                        {
                                // Escape any pipe characters before storing
                                $level1[$k1]    = str_replace('|', '\|', $v1);
                                $level1[$k1]    = str_replace(array("\r\n", "\n"), '\\n', $v1);
                        }
 
                        // Implode the array to store
                        $prepend        .= $key."=".implode('|', $level1)."\n";
                }
                else
                {
                        // Join lines
                        $level1         = str_replace('|', '\|', $level1);
                        $level1         = str_replace(array("\r\n", "\n"), '\\n', $level1);
                        $prepend        .= $key."=".$level1."\n";
                }
        }
 
        return $prepend."\n".$retval;
}

[<! removed edit link to red link >] <! removed transcluded page call, red link never existed >

Examples

<CodeExamplesForm />