php preg_match_all로 smarty foreach를 재현하십시오
-
22-08-2019 - |
문제
"Smarty Foreach"Comportment를 재현하고 싶습니다.
tpl 파일 컨텐츠는 ($ tplContent)입니다.
{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
(
)
)
{foreach} {/foreach} 사이에 코드를 반환하는 방법은 무엇입니까?
해결책
나는 당신이 무엇을하고 있는지 전혀 이해하지 못하지만 이것은 효과가있는 것 같습니다.
$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);
다른 팁
나는 방법을 찾았다. 문제는 r n으로 온다 :
$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]입니다.
"Chad Birch"에게 감사합니다;)
제휴하지 않습니다 StackOverflow