3

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

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

4

2 回答 2

0
    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;
};
于 2018-12-25T17:28:44.300 回答
-1

使用文档中详述的 C 或 C++ API ,编写一个小程序将 libconfig 格式文件转换为 JSON 或 XML(或者如果您喜欢冒险,可以使用 PHP 的序列化格式),然后使用 PHP 库来处理该输出. 如果文件没有更改,您甚至可以缓存转换后的表单。

您可以从 PHP 调用外部程序并使用exec()获取输出。

最好的解决方案当然是为该库编写 PHP 绑定,但根据该库对您的应用程序的重要性,这可能不值得。

查看格式,我不建议尝试使用正则表达式来解析文件。

于 2013-03-01T23:13:18.390 回答