إنتاج هندى foreach مع بى preg_match_all
-
22-08-2019 - |
سؤال
وأود أن إنتاج "هندى 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);
نصائح أخرى
ولقد وجدت كيف. المشكلة تأتي إلى \ ص \ ن:
$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]
وشكرا ل"تشاد بيرش".)
لا تنتمي إلى StackOverflow