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

search for in the

preg_grep> <PCRE-Funktionen
[edit] Last updated: Sat, 07 Jan 2012

view this page in

preg_filter

(PHP 5 >= 5.3.0)

preg_filterSucht und ersetzt mit regulären Ausdrücken

Beschreibung

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

Davon abgesehen, dass preg_filter() bei Übereinstimmungen nur (möglicherweise umgewandelte) Zeichenketten zurückgibt, ist sie mit preg_replace() identisch. Details über die Funktionsweise dieser Funktion finden Sie in der preg_replace()-Dokumentation.

Rückgabewerte

Gibt ein Array zurück, falls subject ein Array ist, andernfalls eine Zeichenkette.

Falls keine Übereinstimmungen gefunden wurden oder ein Fehler auftrat, wird ein leeres Array zurückgegeben, wenn subject ein Array ist, andernfalls NULL.

Beispiele

Beispiel #1 Beispiel zum Vergleich von preg_filter() und preg_replace()

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

echo 
"preg_filter liefert\n";
print_r(preg_filter($suchmuster$ersatz$zeichenkette));

echo 
"preg_replace liefert\n";
print_r(preg_replace($suchmuster$ersatz$zeichenkette));
?>

Das oben gezeigte Beispiel erzeugt folgende Ausgabe:

preg_filter liefert
Array
(
    [0] => A:C:1
    [1] => B:C:a
    [2] => A:2
    [3] => B:b
    [4] => A:3
    [7] => A:4
)
preg_replace liefert
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
)

Siehe auch



preg_grep> <PCRE-Funktionen
[edit] Last updated: Sat, 07 Jan 2012
 
add a note add a note User Contributed Notes preg_filter
sajina_99 at hotmail dot de 25-Sep-2011 07:07
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 ;
     
    }
   
  }
?>
MrBertie 31-Dec-2010 02:35
Another way to filter an array, and simply return the matching items: preg_grep!
Eric Naeseth 17-Dec-2010 06:38
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