开发者

PHP : simple regex problem

Some of my HTML files contains string like :

{foreach $any_kind_of_charaters}
Any kind of string including "\n\r" and spaces here
{/foreach}

I want to apply PHP's preg_match_all on them and wanna return a nice array like printed below

Array
(
    [0] => {foreach $any_kind_of_charaters}
            Any kind of string including "\n\r" and spaces here
           {/foreach}
    [1] => any_kind_of_charaters
    [2] => Any kind of string including "\n\r" and spaces here
)

This REGEX : /\{foreach\s+\$(.*)\}\s+(.*)\s+\{\/foreach\}/ working okay for me,

but it fails when i add new lines(\n) between {foreach}{/foreach} tags.

You help will be much appreciated, thanks.

ARRAY AFTER USING "S" MODIFIER

Array
(
    [0] => {foreach $any_kind_of_charaters}
        Any kind of string including "\n\r" and spaces here
        {/foreach}
    [1] => any_kind_of_charaters}
        Any kind of string including "\n\r" and spaces here
    [2] => 
)

Look second key of the array contain unnecessary data, and last key of array开发者_如何学Python is totally empty.


Set the s modifier flag on your regular expression.

http://php.net/manual/en/reference.pcre.pattern.modifiers.php


Like this: /\{foreach\s+\$(.*)\}\s+(.*)\s+\{\/foreach\}/s <- note the modifier

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜