2010-01-06 2 views
1

나는 다음과 같은 conf 파일을 가지고 있습니다 : http://pastie.org/768582 그리고 저의 목표는 배열에서 각 키의 설명과 키/값을 얻는 것입니다.PHP (Doxygen 파일)에서 설정 파일을 구문 분석하는 알고리즘

array( array(

'comment' => "The PROJECT_NAME tag is a single", 

    'key' => "PROJECT_NAME", 

    'value' => "JMK", 
), 

)

나는 알고리즘으로 내가 사용해야 할 일을 알아?

나는 explode() 함수를 사용하여 구성 파일의 내용을 이미 한 줄씩 배열로 변환했다.

이제 모든 주석 줄을 가져 오려고합니다. 다음 줄은 '#'과 몇 개의 키/값으로 시작하지만 여기에는 문제가 있습니다.

누군가가 아이디어가 있다면 좋을 것입니다. 고마워.

답변

0

시도해 볼 수 있습니다 parse_ini_file(), 형식이 호환됩니다. 그러나 댓글을 처리하지는 않습니다. 이것은 당신에게 키/값 쌍을 얻을 것이다

+0

불행하게도 파일이 보인다되지 않습니다 잘 형성되어야한다. ';'대신 '#'주석을 확인하십시오. '('와 같은 첫 번째 특수 문자는 오류를 출력합니다.) 내가하지 않을 것 같아요 . – toddoon

+0

Infact doxygen 설정 파일이 Makefile과 유사하게 보입니다. PHP가 Makefile을 쉽게 파싱 할 수 있는지 살펴 보겠습니다. – toddoon

1

있지만 코멘트 :

$options = array(); 

foreach ($line as $l) 
{ 
    $l = trim($l); 
    if (strlen($l) && substr($l, 0, 1) != '#') 
    { 
    list($key, $value) = explode("=", $l); 

    // remove whitespace from the end of the config key 
    $key = rtrim($key); 

    $options[$key] = $value; 
    } 
} 
1

여기에 한 가지 방법

$content = file_get_contents("file"); 
$s = preg_split("/#--*/",$content); 
$y = preg_split("/\n\n/",end($s)); 
for($i=0;$i<count($y)-1;$i++){ 
    if ($y[$i]){ 
     if (strpos($y[$i],"#")!==FALSE){ 
      $comment="$y[$i]\n"; 
      $conf=$y[$i+1]; 
      $cs = array_map(trim,explode("=",$conf)); 
      $A["comment"]=$comment; 
      $A["key"]=$cs[0]; 
      $A["value"]=$cs[1]; 
      $TA[]=$A; 
     } 
    } 
} 
print_r($TA); 

출력

Array 
(
    [0] => Array 
     (
      [comment] => # This tag specifies the encoding used for all characters in the config file 
# that follow. The default is UTF-8 which is also the encoding used for all 
# text before the first occurrence of this tag. Doxygen uses libiconv (or the 
# iconv built into libc) for the transcoding. See 
# http://www.gnu.org/software/libiconv for the list of possible encodings. 

      [key] => DOXYFILE_ENCODING 
      [value] => UTF-8 
     ) 

    [1] => Array 
     (
      [comment] => # The PROJECT_NAME tag is a single word (or a sequence of words surrounded 
# by quotes) that should identify the project. 

      [key] => PROJECT_NAME 
      [value] => JMK 
     ) 

    [2] => Array 
     (
      [comment] => # The PROJECT_NUMBER tag can be used to enter a project or revision number. 
# This could be handy for archiving the generated documentation or 
# if some version control system is used. 

      [key] => PROJECT_NUMBER 
      [value] => 10 
     ) 

) 
관련 문제