Re: [问题] 匹配多行

楼主: godspeedlee (妳,我可以)   2011-09-11 15:09:18
※ 引述《pm2002 (pm2002)》之铭言:
: <?php
: $text = '<text>
: 这是第一行
: 这是第二行
: 这是第三行
: </text>';
: preg_match_all('@<text>([.\n]+)</text>@',$text,$result,PREG_SET_ORDER);
: echo $result[0][1];
: ?>
: 以上述为例,我想取出<text></text>标签包起来的内容
: 维基 regex 条目的其中一条内文:
: . 匹配除“\n”之外的任何单个字符。
: 要匹配包括“\n”在内的任何字符,请使用像“[.\n]”的模式。
: 如果不加s modifier的话
: 我用 [\s\S]+ 可以正常抓的到
: 为什么用 [.\n]+ 或 [.\r\n]+ 就抓不到?
参考:
http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php
添加 modifier: s (PCRE_DOTALL), i (PCRE_CASELESS)
得到:
'/<text>(.*?)<\/text>/is'
另外如果是为了解 HTML,PHP 也有 DOM API 可用:
http://php.net/manual/en/book.dom.php

Links booklink

Contact Us: admin [ a t ] ucptt.com