note 87605 added to function.parse-ini-file
| From: | (gmail) | Date: | Fri, 12 Dec 2008 16:42:59 +0000 |
| Subject: | note 87605 added to function.parse-ini-file | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-148082@lists.php.net to get a copy of this message | ||
To those who were like me looking if this could be used to create an array out of commandline output
I offer you the function below (I used it to parse mplayer output).
If you want it behave exactly the same as parse_ini_file you'll obviously have to add some code
to feed the different sections to this one. Hope it's of help to someone!
<?php
/**
* The return is very similar to that of parse_ini_file, but this works off files
*
* Below is an example of what it does, where the first
* value is what you'd normally want to do, and the second and third things that might
* happen and in case it does it's good to know what is going on.
*
* $anArray = array( 'default=theValue', 'setting=',
'something=value=value' );
* explodeExplode( '=', $anArray );
*
* the return will be
* array( 'default' => 'theValue', 'setting' => '',
'something' => 'value=value' );
*
* So the oddities here are, text after the second $string occurence dissapearing
* and empty values resulting in an empty string.
*
* @return $returnArray array array( 'setting' => 'value' )
* @param $string Object
* @param $array Object
*/
function explodeExplode( $string, $array )
{
$returnArray = array();
foreach( $array as $arrayValue )
{
$tmpArray = explode( $string, $arrayValue );
if( count( $tmpArray ) == 1 )
{
$returnArray[$tmpArray[0]] = '';
}
else if( count( $tmpArray ) == 2 )
{
$returnArray[$tmpArray[0]] = $tmpArray[1];
}
else if( count( $tmpArray ) > 2 )
{
$implodeBack = array();
$firstLoop = true;
foreach( $tmpArray as $tmpValue )
{
if( $firstLoop )
{
$firstLoop = false;
}
else
{
$implodeBack[] = $tmpValue;
}
}
print_r( $implodeBack );
$returnArray[$tmpArray[0]] = implode( '=', $implodeBack );
}
}
return $returnArray;
}
?>
----
Server IP: 89.248.170.61
Probable Submitter: 82.169.45.174
----
Manual Page -- http://www.php.net/manual/en/function.parse-ini-file.php
Edit -- https://master.php.net/note/edit/87605
Del: integrated -- https://master.php.net/note/delete/87605/integrated
Del: useless -- https://master.php.net/note/delete/87605/useless
Del: bad code -- https://master.php.net/note/delete/87605/bad+code
Del: spam -- https://master.php.net/note/delete/87605/spam
Del: non-english -- https://master.php.net/note/delete/87605/non-english
Del: in docs -- https://master.php.net/note/delete/87605/in+docs
Del: other reasons-- https://master.php.net/note/delete/87605
Reject -- https://master.php.net/note/reject/87605
Search -- https://master.php.net/manage/user-notes.php