2009-04-02 5 views
0

"Smarty foreach"콤포션을 재현하고 싶습니다.Php preg_match_all로 Smarty foreach 재현

{foreach from=$tabMethodTest item=entry} 
    /** 
    * @todo Implement test{$entry.name}(). 
    */ 
    public function test{$entry.name}() { 
     $this->markTestIncomplete("This test has not been implemented yet."); 
    } 
{/foreach} 

으로, preg_match_all 코드는 다음과 같습니다 :

preg_match_all("/(.*)\{foreach(.*)\}(.*)\{\/foreach\}(.*)/im",$tplContent,$regsTplResult); 
print_r($regsTplResult); 

에서 print_r 반환 :

Array 
(
    [0] => Array 
     (
     ) 

    [1] => Array 
     (
     ) 

    [2] => Array 
     (
     ) 

    [3] => Array 
     (
     ) 

    [4] => Array 
     (
     ) 

) 

내가 코드를 반환 할 수있는 방법

TPL 파일 내용은 ($ tplContent)입니다 {foreach} {/ foreach} 사이에요?

답변

0

은 정말 전혀 무슨 일을하는지 이해가 안 돼요,하지만이 작동하는 것 같다 : 내가 어떻게가 발견

$tplContent = "{foreach from=\$tabMethodTest item=entry}\nHello\n{/foreach}"; 
$pattern = '/\{foreach from=\$tabMethodTest item=entry\}[\r\n]{1,2}(.*)[\r\n]{1,2}\{\/foreach\}/im'; 
preg_match_all($pattern,$tplContent,$regsTplResult); 
print_r($regsTplResult); 
+0

이 패턴이 작동하지 않습니다. ( –

+0

죄송합니다, 구문이 좋지 않아서 테스트를 마친 후 잠시 후 편집하겠습니다. –

+0

죄송합니다, 괜찮습니다.하지만 진짜라고 생각합니다. probes를 만든 콘텐츠를 사용합니다. 흠. 나는 나의 질문을 새롭게한다. –

0

.

$pattern = '/\{foreach from=\$tabMethodTest item=entry\}(.*)\{\/foreach\}/im'; 
$tplContent = preg_replace("/[\n\r]/","",$tplClassTestContent); 
preg_match_all($pattern,$tplContent,$regsTplResult); 
print_r($regsTplResult); 

결과는 다음과 같습니다 :

Array 
(
    [0] => Array 
     (
      [0] => {foreach from=$tabMethodTest item=entry} /**  * @todo Implement test{$entry.name}().  */ public function test{$entry.name}() {  $this->markTestIncomplete("This test has not been implemented yet."); } {/foreach} 
     ) 

    [1] => Array 
     (
      [0] => /**  * @todo Implement test{$entry.name}().  */ public function test{$entry.name}() {  $this->markTestIncomplete("This test has not been implemented yet."); }  
     ) 

) 

내가 차드 "에 $ regsTplResult [1] [0]

감사에 원하는 결과 문제는 연구 \ n을 \ 온다 자작 나무 ";)