preg_filter

(PHP 5 >= 5.3.0)

preg_filterПроизводит поиск и замену по регулярному выражению

Описание

mixed preg_filter ( mixed $pattern , mixed $replacement , mixed $subject [, int $limit = -1 [, int &$count ]] )

Функция preg_filter() идентична функции preg_replace() за исключением того, что возвращает только те значения (возможно, преобразованные), в которых найдено совпадение. Подробнее о работе функции читайте в документации к preg_replace().

Возвращаемые значения

Возвращает array, если аргумент subject имеет тип array, или string в противном случае.

Если совпадений не найдено или возникла ошибка, возвращается пустой массив array, когда subject имеет тип array, или NULL в противном случае.

Примеры

Пример #1 Пример для сравнения функций preg_filter() и preg_replace()

<?php
$subject 
= array('1''а''2''б''3''А''Б''4'); 
$pattern = array('/\d/''/[а-я]/''/[1а]/'); 
$replace = array('А:$0''Б:$0''В:$0'); 

echo 
"preg_filter возвращает\n";
print_r(preg_filter($pattern$replace$subject)); 

echo 
"preg_replace возвращает\n";
print_r(preg_replace($pattern$replace$subject)); 
?>

Результат выполнения данного примера:

preg_filter возвращает
Array
(
    [0] => А:В:1
    [1] => Б:В:а
    [2] => А:2
    [3] => Б:б
    [4] => А:3
    [7] => А:4
)
preg_replace возвращает
Array
(
    [0] => А:В:1
    [1] => Б:В:а
    [2] => А:2
    [3] => Б:б
    [4] => А:3
    [5] => А
    [6] => Б
    [7] => А:4
)

Смотрите также

  • Шаблоны PCRE
  • preg_replace() - Выполняет поиск и замену по регулярному выражению
  • preg_replace_callback() - Выполняет поиск по регулярному выражению и замену с использованием callback-функции
  • preg_grep() - Возвращает массив вхождений, которые соответствуют шаблону
  • preg_last_error() - Возвращает код ошибки выполнения последнего регулярного выражения PCRE

Коментарии

Автор:
Another way to filter an array, and simply return the matching items: preg_grep!
2010-12-31 04:35:15
http://php5.kiev.ua/manual/ru/function.preg-filter.html
As I had to work with PHP5.2.X and needed preg_filter I wrote a quick and dirty workaround.

<?php
 
if (!function_exists('preg_filter')) {
 
    function 
preg_filter($pattern$replace$subject$limit = -, &$count null) {
   
      if(!
is_array($subject)) {
       
$noArray ;
       
$subject = array($subject);
      }

     
$preg preg_replace($pattern$replace$subject$limit,  &$count);

     
$diff array_diff($preg$subject);
     
      if(
$noArray == 1$diff implode($diff) ;

      return 
$diff ;
     
    }
   
  }
?>
2011-09-25 10:07:08
http://php5.kiev.ua/manual/ru/function.preg-filter.html
Автор:
As for the php 5.5.3 version, the example will turn out like:
Array
(
       [0]=>A:1
       [1]=>B:a
       ...
)
2014-03-04 12:36:07
http://php5.kiev.ua/manual/ru/function.preg-filter.html
As for the php 5.5.3 version, the example  turn out like:
Array
(
       [0] => A:1
       [1] => B:a
       ...
)
2014-03-04 12:44:55
http://php5.kiev.ua/manual/ru/function.preg-filter.html
For those who read manual not in English and figure out that example code returns not the same result as in documentation

You need put in example patter modifier "u" to get same result:

<?php
$subject 
= array('1''а''2''б''3''А''Б''4');
// Added "u"
$pattern = array('/\d/u''/[а-я]/u''/[1а]/u');
$replace = array('А:$0''Б:$0''В:$0');
?>
2016-10-20 16:14:03
http://php5.kiev.ua/manual/ru/function.preg-filter.html
If you don't want to actually replace the matches, just filter the array to them, make the replacement parameter '$0'.

<?php
$subject 
= array('1''a''2''b''3''A''B''4'); 
$result preg_filter('/\d/''$0'$subject);  // ['1','2','3','4']
?>
2017-10-28 00:10:24
http://php5.kiev.ua/manual/ru/function.preg-filter.html

    Поддержать сайт на родительском проекте КГБ