gpt4 book ai didi

php - 在 PHP 中解析 libconfig 文件

转载 作者:行者123 更新时间:2023-12-01 15:34:58 28 4
gpt4 key购买 nike

我需要在 PHP 中从“libconfig”格式的文件中读取和写入变量。但我找不到任何地方的图书馆。我当然知道 C/C++ 库,但我们必须编写扩展才能使用它。

是否存在这样的库或扩展?

最佳答案

    function parseLibconfig_section( &$Array, &$LineID ){
$RetVal = array(); // Initializing return value as empty array
while( count( $Array ) > $LineID ){ // While not riches last line
if( stripos( $Array[$LineID] , ':' ) !== false ){
// In case we have section Title - just remember it
// The section will parsed later at section begin (next loop)
$TArr = explode( ' ', trim( $Array[$LineID] ) );
$CS = $TArr[0];
} elseif( stripos( $Array[$LineID] , '{' ) !== false ){
// We at section open Tag -> call recurrent function to parse
// from next line of input data
$LineID++;
$RetVal[$CS] = parseLibconfig_section( $Array, $LineID );
} elseif( stripos( $Array[$LineID] , '}' ) !== false ){
// End of section - return back from subsection
break;
} else {
// nor section begin/ nor section end - parse line as field
// by standard PHP function parse_ini_string (please see PHP ref)
$TVrr = parse_ini_string( trim( $Array[$LineID] ) );
if( count( $TVrr ) ){
// fill return array by fields from parse_ini_string function
foreach( $TVrr as $Key => $Val ) $RetVal[$Key] = $Val;
};
};
// Next please!
$LineID++;
};
return $RetVal;
};

function parseLibconfig( $FName ){
$RetVal = array(); // Initializing return value as empty array
$Data = file( $FName ); // Reading content of libconfig's
// config file into array of lines
if( count($Data)> 0 ){ // If we have some data read then - working
$Index = 0; // Init an variable to pass by reference into
// function that will be called recursively
$RetVal = parseLibconfig_section( $Data, $Index );
};
return $RetVal;
};

关于php - 在 PHP 中解析 libconfig 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9874522/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com