|
PODRĘCZNIK PHP 5.x, 4.x, 3.x - częściowo spolszczony / źródło: www.php.net
[Spis]
[A]
[B]
[C]
[D]
[E]
[F]
[G]
[H]
[I]
[J]
[K]
[L]
[M]
[N]
[O]
[P]
[Q]
[R]
[S]
[T]
[U]
[V]
[X]
[W]
[Z]
preg_grep (PHP 4, PHP 5) preg_grep --
Return array entries that match the pattern
Descriptionarray preg_grep ( string pattern, array input [, int flags] )
preg_grep() returns the array consisting of
the elements of the input array that match
the given pattern.
flags can be the following flag:
- PREG_GREP_INVERT
If this flag is passed, preg_grep() returns the
elements of the input array that do not match
the given pattern.
This flag is available since PHP 4.2.0.
Since PHP 4.0.4, the results returned by preg_grep()
are indexed using the keys from the input array. If this behavior is
undesirable, use array_values() on the array returned by
preg_grep() to reindex the values.
Przykład 1. preg_grep() example |
<?php
$fl_array = preg_grep("/^(\d+)?\.\d+$/", $array);
?>
|
|
User Contributed Notesak85 at yandex dot ru
03-Aug-2005 07: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
30-Apr-2005 06: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 09:05
To grep using preg_* from a string instead of an array, use preg_match_all().
zac at slac dot stanford dot edu
27-Jul-2002 04: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
)
|