PHP的 preg_match_all
生活随笔
收集整理的這篇文章主要介紹了
PHP的 preg_match_all
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
語法:int?preg_match_all?( string pattern, string subject, array &matches [, int flags] )
這個函數的返回值是整個pattern匹配到的結果數量。
matches(注意這里是引用)是一個二維數組,matches[0] 是整個pattern匹配的結果。matches[1] 是第一個子模式(正則表達式中的子模式,就是圓括號括起來的部分)匹配的結果,matches[2]是 第二個子模式的匹配結果,依次推matches[N]是 第N個子模式的匹配結果。
說明:有N個括號,就對應有N個子模式,子模式的元素就是括號中匹配到的內容。子模式中元素的個數=正則匹配到的結果的個數。
舉例:
<?php $pattern = "/<[^>]+>(.*)<\/[^>]+>/U";$subject = "<b>example:</b><div align = left>this is a test</div>";preg_match_all($pattern,$subject,$out);print_r($out); ?>?
結果:
$pattern 正則在字符串中匹配到了2個滿足條件的結果,分別是
匹配結果1: <b>example:</b>匹配結果2: <div align = left>this is a test</div>所以每隔大數組里面的元素個數都是2,并且元素之間都是對應的(通過數組的索引鍵)
注意:這個結果的形式,array[0]是整個pattern匹配到的結果,array[1]是pattern中子模式 (.*) 所匹配到的結果。但是array[1]匹配的結果是和array[0]中的結果相對應
?
轉載于:https://www.cnblogs.com/echojson/p/10749493.html
總結
以上是生活随笔為你收集整理的PHP的 preg_match_all的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Matrix Completion wi
- 下一篇: 【share】PHP站点用Squid再次