0

我想重现“Smarty foreach”的评论。

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} 之间返回代码?

4

2 回答 2

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);
于 2009-04-02T18:59:18.410 回答
0

我找到了方法。问题来了 \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]

感谢“乍得桦树”;)

于 2009-04-02T20:08:12.363 回答