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

search for in the

preg_replace> <preg_quote
Last updated: Fri, 18 Jul 2008

view this page in

preg_replace_callback

(PHP 4 >= 4.0.5, PHP 5)

preg_replace_callback — Esegue ricerche e sostituzioni con espressioni regolari usando il callback

Descrizione

mixed preg_replace_callback ( mixed $espressione_regolare , callback $callback , mixed $testo [, int $limite ] )

Fondamentalmente questa funzione si comporta come preg_replace(), eccetto che per la presenza del callback . Con quest'ultimo parametro si indica una funzione da richiamare a cui verrà passata una matrice con i testi riconosciuti in testo . La funzione di callback dovrebbe restituire la stringa da sostituire.

Example #1 Esempio dell'uso di preg_replace_callback()

<?php 
// questo test è stato usato nel 2002
// lo si vuole avere aggiornato per il 2003
$text "April fools day is 04/01/2002\n"
$text.= "Last christmas was 12/24/2001\n"
// la funzione di callback
function next_year($matches

// come solito: $matches[0] è il testo riconosciuto completo
// $matches[1] la parte riconosciuta per il primo criterio
// racchiuso in '(...)' e così via
return $matches[1].($matches[2]+1); 
}
echo 
preg_replace_callback(
     
"|(\d{2}/\d{2}/)(\d{4})|",
     
"next_year",
     
$text);
// il risultato sarà:
// April fools day is 04/01/2003
// Last christmas was 12/24/2002
?>

Spesso si ha la necessità di richiamare la funzione callback soltanto in un unico posto. In questo caso si può utilizzare create_function() per dichiarare una funzione anonima come callback per preg_replace_callback().In questo modo si hanno tutte le informazioni per la chiamata in un unico posto e non si disperde con funzioni di callback non utilizzate altrove.

Example #2 preg_replace_callback() e create_function()

<?php
/* Un filtro da linea di comando stile Unix che converte
 * la maiuscole poste all'inizio delle parole in minuscolo */
$fp fopen("php://stdin""r") or die("can't read stdin");
while (!
feof($fp)) {
      
$line fgets($fp);
      
$line preg_replace_callback(
             
'|<p>\s*\w|',
             
create_function(
                  
// l'apice singolo è essenziale qui, 
                  // o in alternativa occorre usare la sequenza di escape \$ 
                  // per tutte le occorrenze di $ 
                 
'$matches',
                 
'return strtolower($matches[0]);' 
                 
), 
               
$line 
      
);
      echo 
$line
}
fclose($fp);
?>

Vedere anche preg_replace() create_function(), e informazioni sul tipo callback.



preg_replace> <preg_quote
Last updated: Fri, 18 Jul 2008
 
add a note add a note User Contributed Notes
preg_replace_callback
nene at triin dot net
20-May-2008 12:14
The first example is bad, because it creates function for every line it processes. When the file has many lines, you could easily run out of memory. The code should be changed so, that create_function() is used outside of loop.
Sjon at hortensius dot net
24-Jun-2007 01:56
preg_replace_callback returns NULL when pcre.backtrack_limit is reached; this sometimes occurs faster then you might expect. No error is raised either; so don't forget to check for NULL yourself
matt at mattsoft dot net
26-Apr-2006 11:16
it is much better on preformance and better practice to use the preg_replace_callback function instead of preg_replace with the e modifier.

function a($text){return($text);}

// 2.76 seconds to run 50000 times
preg_replace("/\{(.*?)\}/e","a('\\1','\\2','\\3',\$b)",$a);

// 0.97 seconds to run 50000 times
preg_replace_callback("/\{(.*?)\}/s","a",$a);

preg_replace> <preg_quote
Last updated: Fri, 18 Jul 2008
 
 
show source | credits | sitemap | contact | advertising | mirror sites