Ran*_*oor 0 php regex preg-match-all
我的一些HTML文件包含如下字符串:
{foreach $any_kind_of_charaters}
Any kind of string including "\n\r" and spaces here
{/foreach}
Run Code Online (Sandbox Code Playgroud)
我想申请PHP's preg_match_all他们,并希望返回一个array像下面打印的好
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
)
Run Code Online (Sandbox Code Playgroud)
这个REGEX:/\{foreach\s+\$(.*)\}\s+(.*)\s+\{\/foreach\}/对我来说还可以,
但是当我在{foreach}{/foreach}标签之间添加新行(\n)时它会失败.
非常感谢您的帮助,谢谢.
使用"S"修改器后的阵列
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] =>
)
Run Code Online (Sandbox Code Playgroud)
看second key的array包含不必要的数据,和last key的array完全是空的.
s在正则表达式上设置修饰符标志.
http://php.net/manual/en/reference.pcre.pattern.modifiers.php
像这样:/\{foreach\s+\$(.*)\}\s+(.*)\s+\{\/foreach\}/s< - 注意修饰符