views:

833

answers:

2

Hi, I would like to reproduce the "Smarty foreach" comportment.

The tpl file content is ($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}

The preg_match_all code is :

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

The print_r return :

Array
(
    [0] => Array
        (
        )

    [1] => Array
        (
        )

    [2] => Array
        (
        )

    [3] => Array
        (
        )

    [4] => Array
        (
        )

)

How i can return the code between {foreach}{/foreach} ?

A: 

I don't really understand what you're doing at all, but this seems to work:

$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);
Chad Birch
This pattern doesn't work :(
Kevin Campion
Sorry, my syntax must be bad, I'll edit in a minute after I've actually tested.
Chad Birch
Sorry, it's ok, but i think it's the real content i use which created a problem. I refresh my question.
Kevin Campion
I think the problem come to spaces and comments
Kevin Campion
A: 

I found how to. The problem comes to \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);

The result is :

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.");    } 
        )

)

The result that i want is in $regsTplResult[1][0]

Thank to "Chad Birch" ;)

Kevin Campion