preg_grep
返回匹配模式的數組條目
preg_grep()
函數返回一個數組,其中僅包含輸入中與給定模式匹配的元素。
從數組中獲取以"p" 開頭的項:
<?php $input = [ "Red" , "Pink" , "Green" , "Blue" , "Purple" ] ; $result = preg_grep ( "/^p/i" , $input ) ; print_r ( $result ) ; ?>
親自試一試
preg_grep ( pattern , input , flags )
參數 | 描述 |
---|---|
pattern | 必需的。包含指示要搜索的內容的正則表達式。 |
input | 必需的。字符串數組。 |
flags | 選修的。此函數只有一個標誌。傳遞常量PREG_GREP_INVERT 將使函數僅返回與模式不匹配的項。 |