preg_grep

(PHP 4, PHP 5)

preg_grep --  Возвращает массив вхождений, которые соответствуют шаблону

Описание

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

preg_grep() возвращает массив, состоящий из элементов входящего массива input, которые соответствуют заданному шаблону pattern.

Параметр flags может принимать следующие значения:

PREG_GREP_INVERT

В случае, если этот флаг установлен, функция preg_grep(), возвращает те элементы массива, которые не соответствуют заданному шаблону pattern. Этот флаг доступен, начиная с PHP 4.2.0.

Начиная с PHP 4.0.4, результат, возвращаемый функцией preg_grep() использует те же индексы, что и массив исходных данных. Если такое поведение вам не подходит, примените array_values() к массиву, возвращаемому preg_grep() для реиндексации.

Пример 1. preg_grep() пример

<?php
// Возвращает все элементы массива,
// содержащие числа с плавающей точкой
$fl_array = preg_grep("/^(\d+)?\.\d+$/", $array);
?>



preg_grep
tobyf at web dot de
24-May-2006 12:39
For older PHP versions (> 4.2.0) you can use this function:

<?php
function c_preg_grep($pattern,&$array,$flag=false)
{
   if (
$flag != PREG_GREP_INVERT)
   {
       return
preg_grep($pattern,$array);
   }
   else
   {
      
$remove = preg_grep($pattern,$array);
       return
array_diff($array,$remove);
   }
}
?>
ak85 at yandex dot ru
02-Aug-2005 10:28
If U wanna find substring without word "BADWORD" U can use this expression:
/((?:(?!BADWORD).)*)/s
For example:
preg_match_all('/<b>((?:(?!</b>).)*)</b>/is',$string,$matches);
// U can find at the $matches substrings of $string between '<b>' and '</b>' without the same tag '</b>'

// Was found at O'Reilly Rerl Cookbook Russian Edition 2001
erik dot dobecky at NOSPAM dot fi-us dot com
29-Apr-2005 09:15
A useful way that we have developed filters against SQL injection attempts is to preg_grep the $_REQUEST global with the following regular expression (regex):

   '/[\'")]* *[oO][rR] *.*(.)(.) *= *\\2(?:--)?\\1?/'

which is used simply as:
<?php
$SQLInjectionRegex
= '/[\'")]* *[oO][rR] *.*(.)(.) *= *\\2(?:--)?\\1?/';
$suspiciousQueryItems = preg_grep($SQLInjectionRegex, $_REQUEST);
?>

which matches any of the following (case insensitive, a=any char) strings (entirely):

' or 1=1--
" or 1=1--
or 1=1--
' or 'a'='a
" or "a"="a
') or ('a'='a
Matt AKA Junkie
06-Jun-2004 12:05
To grep using preg_* from a string instead of an array, use preg_match_all().
zac at slac dot stanford dot edu
26-Jul-2002 07:02
preg_grep takes a third argument (PREG_GREP_INVERT) which negates the pattern matching behaviour, just like the "-v" flag to the grep command.

EXAMPLE:
$food = array('apple', 'banana', 'squid', 'pear');
$fruits = preg_grep("/squid/", $food, PREG_GREP_INVERT);
echo "Food  "; print_r($food);
echo "Fruit "; print_r($fruits);

RESULTS IN:
Food  Array
(
   [0] => apple
   [1] => banana
   [2] => squid
   [3] => pear
)
Fruit Array
(
   [0] => apple
   [1] => banana
   [3] => pear
)

<Синтаксис регулярных выраженийpreg_match_all>
 Last updated: Tue, 15 Nov 2005