downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

preg_grep> <PCRE İşlevleri
[edit] Last updated: Fri, 17 May 2013

view this page in

preg_filter

(PHP 5 >= 5.3.0)

preg_filterBir düzenli ifade arama ve değiştirmesi yapar

Açıklama

mixed preg_filter ( mixed $şablon , mixed $yenisi , mixed $konu [, int $sınır = -1 [, int &$sayı ]] )

preg_filter() sadece eşleşenleri döndürmesi dışında preg_replace() işlevi gibidir. Bu işlevin nasıl çalıştığıyla ilgili ayrıntılar için preg_replace() belgesini okuyun.

Değiştirgeler

şablon

yenisi

konu

sınır

sayı

Dönen Değerler

konu değiştirgesi bir dizi ise bir dizi döndürür aksi takdirde bir dizge döner.

Eşleşmeler bulunduğu takdirde yeni konu döner, aksi takdirde bir hata oluşmamışsa konu değiştirilmeden döndürülür; bir hata oluşmuşsa NULL döner.

Örnekler

Örnek 1 - preg_filter() ile preg_replace() işlevini karşılaştırma örneği

<?php
$subject 
= array('1''a''2''b''3''A''B''4');
$pattern = array('/\d/''/[a-z]/''/[1a]/');
$replace = array('A:$0''B:$0''C:$0');

echo 
"preg_filter işlevinin sonucu:\n";
print_r(preg_filter($pattern$replace$subject));

echo 
"preg_replace  işlevinin sonucu:\n";
print_r(preg_replace($pattern$replace$subject));
?>

Yukarıdaki örneğin çıktısı:

preg_filter işlevinin sonucu:
Array
(
    [0] => A:C:1
    [1] => B:C:a
    [2] => A:2
    [3] => B:b
    [4] => A:3
    [7] => A:4
)
preg_replace işlevinin sonucu:
Array
(
    [0] => A:C:1
    [1] => B:C:a
    [2] => A:2
    [3] => B:b
    [4] => A:3
    [5] => A
    [6] => B
    [7] => A:4
)

Ayrıca Bakınız



preg_grep> <PCRE İşlevleri
[edit] Last updated: Fri, 17 May 2013
 
add a note add a note User Contributed Notes preg_filter - [3 notes]
up
1
MrBertie
2 years ago
Another way to filter an array, and simply return the matching items: preg_grep!
up
1
sajina_99 at hotmail dot de
1 year ago
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 = -1 , &$count = null) {
   
      if(!
is_array($subject)) {
       
$noArray = 1 ;
       
$subject = array($subject);
      }

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

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

      return
$diff ;
     
    }
   
  }
?>
up
-1
Eric Naeseth
2 years ago
If you want to just filter an array based on a regular expression, without replacing any of the array values, try RegexIterator: http://us3.php.net/manual/en/class.regexiterator.php

 
show source | credits | stats | sitemap | contact | advertising | mirror sites