PHP | preg_grep() Function Last Updated : 08 Jun, 2018 Comments Improve Suggest changes Like Article Like Report The preg_grep() is an inbuilt function in PHP. It returns the array consisting of the elements of the input array that match with the given pattern. Syntax : array preg_grep ( $pattern, $input [, $flags] ) Parameters Used: The preg_grep() function take three parameters that are described below: $pattern: The $pattern is an string element which is searched in string array. $input: The $input is the original string array. $flags: The $flags is used for signalize and its variable type used for indicates two states True or False to control the program. If the flag is set to PREG_GREP_INVERT, the function returns elements of the input array that do not match to the given pattern. Return Values: The function returns array indexed using the keys from the input array. Program 1: php <?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); ?> Output: Array ( [1] => for ) Program 2: Take an example of PREG_GREP_INVERT, which is invert data instead of output numbers to be non-numeric values in php. 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); ?> Output: Array ( [1] => one [3] => two [4] => three [7] => Six [9] => Eight [10] => Nine ) Program 3: Example where no match found, then it return NULL array. php <?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); ?> Output: Array ( ) References :http://php.net/manual/en/function.preg-grep.php Comment More infoAdvertise with us Next Article PHP | preg_grep() Function jit_t Follow Improve Article Tags : Web Technologies PHP PHP-function Similar Reads PHP prev() Function The prev() function is an inbuilt function in PHP. It is used to return the immediate previous element from an array of the element which is currently pointed by the internal pointer. We have already discussed current() function in PHP. The current() function is used to return the value of the eleme 2 min read PHP | preg_match() Function This function searches string for pattern, returns true if pattern exists, otherwise returns false. Usually search starts from beginning of subject string. The optional parameter offset is used to specify the position from where to start the search. Syntax: int preg_match( $pattern, $input, $matches 2 min read PHP | parse_url() Function The parse_url() function is an inbuilt function in PHP which is used to return the components of a URL by parsing it. It parses an URL and return an associative array which contains its various components. Syntax: parse_url( $url, $component = -1 ) Parameters: This function accepts two parameters as 2 min read PHP | str_ireplace() Function The str_ireplace() is a built-in function in PHP and is used to replace all the occurrences of the search string or array of search strings by replacement string or array of replacement strings in the given string or array respectively. This function performs the search in a case-insensitive manner. 3 min read PHP | preg_split() Function The preg_split() function is an inbuilt function in PHP which is used to convert the given string into an array. The function splits the string into smaller strings or sub-strings of length which is specified by the user. If the limit is specified then small string or sub-strings up to limit return 3 min read PHP | gmp_strval() Function The gmp_strval() is an inbuilt function in PHP which returns the string value of a GMP number. (GNU Multiple Precision: For large numbers). Syntax: string gmp_strval ( GMP $num, int $base ) Parameters: The function accepts two parameters $num and $base as shown above and described below. $num - The 3 min read PHP get_resources() Function The get_resources() function is an inbuilt function in PHP that returns active resources in an array form, & optionally, the resource type will be filtered. Syntax: array get_resources(?string $type = null)Parameters: This function accepts one parameter that is described below: type: This parame 1 min read PHP | preg_filter() Function The preg_filter() function is an inbuilt function in PHP which is used to perform a regular expression search and replace the text. Syntax: preg_filter( $pattern, $replacement, $subject, $limit, $count ) Parameters: This function accepts five parameters as mention above and describe below. $pattern: 2 min read PHP | xml_parse() Function The xml_parse() function is an inbuilt function in PHP which is used to parse XML document. Syntax:Â int xml_parse( resource $xml_parser, string $xml_data, bool $is_final ) Parameter: This function accepts three parameters as mentioned above and described below:Â Â $xml_parser: It is required paramet 3 min read PHP | is_string() Function The is_string() function is an inbuilt function in PHP which is used to check whether the given value is a string or not. Syntax: bool is_string( mixed $var ) Parameters: This function accepts one parameter as mentioned above and described below: $var: It contains the value that need to check. Retur 1 min read Like