2014-11-12 6 views
4

전자 메일 주소를 얻기 위해 구문 분석하는 PHP 배열이 있습니다. 때로는 테스트에 다른 값을 사용할 수 있도록 항목을 주석으로 추가하려고합니다.구문 분석 할 때 주석 처리 된 줄은 어떻게 무시합니까?

다음은 배열의 예 :

parse read %config.php [ 
    thru "'system.email'" [ 
     thru "'to'" [thru "'workflow'" [thru "'" copy recipient-email to "'^/"]] 
    ] to end 
] 

내가 recipient-email의 값이 것을 실행하면 "[email protected]"

array('system.email' => array(
    'to' => array(
     'contactus' => '[email protected]', 
     'newregistration' => '[email protected]', 
     'requestaccess' => '[email protected]', 
//   'workflow' => '[email protected]' 
     'workflow' => '[email protected]' 
    ) 
)); 

여기 내 구문 분석 규칙입니다. //으로 시작하는 줄을 무시하도록 규칙을 작성하려면 어떻게해야합니까?

답변

5

과 같이 보일 것이다 주석 행을 먹는 규칙 :

spacer: charset reduce [tab cr newline #" "] 
spaces: [some spacer] 
any-spaces: [any spacer] 

[any-spaces "//" thru newline] 

당신은 당신이 당신의 현재의 규칙과 그렇게 할 방법을 판단 할 수 있습니다. 배열의 주석 만 처리하는 다소 엉망인 방법이 있습니다.

text: {array('system.email' => array(              
    'to' => array(                 
     'contactus' => '[email protected]',          
     'newregistration' => '[email protected]',         
     'requestaccess' => '[email protected]',        
//   'workflow' => '[email protected]'         
     'workflow' => '[email protected]'         
    )                    
));} 

list: [] 

spacer: charset reduce [tab cr newline #" "] 
any-spaces: [any spacer] 

comment-rule: [any-spaces "//" thru newline] 

email-rule: [ 
    thru "'" 
    copy name to "'" skip 
    thru "'" 
    copy email to "'" 
    thru newline 
] 

system-emails: [ 
    thru "'system.email'" [ 
     thru "to' => array(" 
     some [ 
      comment-rule | 
      email-rule (append list reduce [name email]) 
     ]  
    ] to end 
] 

parse text system-emails 
print list 

이렇게하면 배열의 이름과 이메일 블록이됩니다.

아마도 전체 론적 접근 방식을 사용하여 소스를 처리하고 구문 분석 전에 모든 주석을 제거 할 수 있습니다. 이것은 내가 과거에 사용한 기능입니다 :

decomment: func [str [string!] /local new-str com comment-removing-rule] [ 

    new-str: copy "" 

    com: [ 
     "//" 
     thru newline 
    ] 
    comment-removing-rule: [ 
     some [ 
      com | 
      copy char skip (append new-str char) 
     ] 
    ] 

    parse str comment-removing-rule 
    return new-str 
] 
+0

그런 포괄적 인 답변에 감사드립니다! 나는 분해 옵션이 더 좋다고 생각한다. –

+1

더 나은 옵션입니다. 나는 PHP를 잘 모른다. (고맙게도!)하지만 주석 규칙에 다른 종류의 주석 (예 :''/ * "~"*/"')을 추가 할 수있다. – kealist