这个 preg_grep() 是PHP中的内置函数。它返回由与给定模式匹配的输入数组元素组成的数组。
null
语法:
array preg_grep ( $pattern, $input [, $flags] )
使用的参数: 这个 preg_grep() 函数采用以下三个参数:
- $pattern: 这个 美元模式 是在字符串数组中搜索的字符串元素。
- $input: 这个 $input 是原始字符串数组。
- $flags: $flags用于signalize,其变量类型用于指示控制程序的两种状态True或False。如果标志设置为 PREG_GREP_倒置 ,该函数返回与给定模式不匹配的输入数组元素。
返回值: 函数返回使用输入数组中的键索引的数组。
项目1:
<?php // PHP program to implement // preg_grep() function // original array elements $inputstrVal = array ( "Geeks" , "for" , "Geeks" , '2018' ); // Search elements "o", followed by one // or more letters. $result =preg_grep ( '/o(w+)/' , $inputstrVal ); print_r( $result ); ?> |
输出:
Array ( [1] => for )
项目2: 以…为例 PREG_GREP_倒置 ,即在php中将数据而不是输出的数字反转为非数值。
<?php // PHP program to implement preg_grep() // function input string $inputstrVal = array (1, "one" , 2, "two" , "three" , 4, 5, "Six" , 7, "Eight" , "Nine" , 10, 11, 12, 13); // Used preg_grep with PREG_GREP_INVERT $result = preg_grep( "/[0-9]/" , $inputstrVal , PREG_GREP_INVERT); // Print result print_r( $result ); ?> |
输出:
Array ( [1] => one [3] => two [4] => three [7] => Six [9] => Eight [10] => Nine )
方案3: 例如,如果未找到匹配项,则返回 无效的 大堆
<?php // PHP program to implement // preg_grep() function //original array elements $inputstrVal = array (0 => "Geeks" , 1 => "for" , 2 => "Geeks" , 3 => '2018' , ); // Search elements "x", followed by one // or more letters. $result =preg_grep ( '/x(w+)/' , $inputstrVal ); print_r( $result ); ?> |
输出:
Array ( )
参考资料: http://php.net/manual/en/function.preg-grep.php
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END