note 97621 deleted from function.parse-ini-string by googleguy
| From: | googleguy@php.net | Date: | Thu, 13 Jun 2013 14:28:52 +0000 |
| Subject: | note 97621 deleted from function.parse-ini-string by googleguy | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-195200@lists.php.net to get a copy of this message | ||
Note Submitter: boukeversteegh at gmail dot com
----
<?php
# Define parse_ini_string if it doesn't exist.
# Does accept lines starting with ; as comments
# Does not accept comments after values
if( !function_exists('parse_ini_string') ){
function parse_ini_string( $string ) {
$array = Array();
$lines = explode("\n", $string );
foreach( $lines as $line ) {
$statement = preg_match(
"/^(?!;)(?P<key>[\w+\.\-]+?)\s*=\s*(?P<value>.+?)\s*$/", $line, $match );
if( $statement ) {
$key = $match[ 'key' ];
$value = $match[ 'value' ];
# Remove quote
if( preg_match( "/^\".*\"$/", $value ) || preg_match(
"/^'.*'$/", $value ) ) {
$value = mb_substr( $value, 1, mb_strlen( $value ) - 2 );
}
$array[ $key ] = $value;
}
}
return $array;
}
}
?>