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