• PHP Video Tutorials

PHP - Function preg_grep()


Advertisements

Syntax

array preg_grep ( string $pattern, array $input [, int $flags] );

Definition and Usage

Returns the array consisting of the elements of the input array that match the given pattern.

If flag is set to PREG_GREP_INVERT, this function returns the elements of the input array that do not match the given pattern.

Return Value

  • Returns an array indexed using the keys from the input array.

Example

Following is the piece of code, copy and paste this code into a file and verify the result.

<?php
   $foods = array("pasta", "steak", "fish", "potatoes");
   
   // find elements beginning with "p", followed by one or more letters.
   $p_foods = preg_grep("/p(\w+)/", $foods);
   
   print "Found food is " . $p_foods[0];
   print "Found food is " . $p_foods[1];
?>

This will produce the following result −

Found food is pastaFound food is
php_regular_expression.htm

Useful Video Courses


Video

PHP Online Training

45 Lectures 9 hours

Malhar Lathkar

Video

Become PHP Facebook Developer: Password-Less Authentication

34 Lectures 4 hours

Syed Raza

Video

Ultimate PHP & MySQL Web Development Course & OOP Coding

84 Lectures 5.5 hours

Frahaan Hussain

Video

Learn PHP from Scratch

17 Lectures 1 hours

Nivedita Jain

Video

E Commerce Website Development In PHP With PDO

100 Lectures 34 hours

Azaz Patel

Video

PHP in Telugu

43 Lectures 5.5 hours

Vijay Kumar Parvatha Reddy

Advertisements