Geen cache-versie.

Caching uitgeschakeld. Standaardinstelling voor deze pagina:ingeschakeld (code LNG204)
Als het scherm te langzaam is, kunt u de gebruikersmodus uitschakelen om de cacheversie te bekijken.

Rechercher une fonction PHP

preg_replace_callback

(PHP 4 >= 4.0.5, PHP 5, PHP 7)

preg_replace_callbackPerform a regular expression search and replace using a callback

Description

preg_replace_callback ( mixed $pattern , callable $callback , mixed $subject [, int $limit = -1 [, int &$count ]] ) : mixed

The behavior of this function is almost identical to preg_replace(), except for the fact that instead of replacement parameter, one should specify a callback.

Eerste pagina van Manuel PHP  Inhoudsopgave Haut

Parameters

pattern

The pattern to search for. It can be either a string or an array with strings.

callback

A callback that will be called and passed an array of matched elements in the subject string. The callback should return the replacement string. This is the callback signature:

handler ( array $matches ) : string

You'll often need the callback function for a preg_replace_callback() in just one place. In this case you can use an anonymous function to declare the callback within the call to preg_replace_callback(). By doing it this way you have all information for the call in one place and do not clutter the function namespace with a callback function's name not used anywhere else.

Example #1 preg_replace_callback() and anonymous function

<?php
/* a unix-style command line filter to convert uppercase
 * letters at the beginning of paragraphs to lowercase */
$fp fopen("php://stdin""r") or die("can't read stdin");
while (!
feof($fp)) {
    
$line fgets($fp);
    
$line preg_replace_callback(
        
'|<p>\s*\w|',
        function (
$matches) {
            return 
strtolower($matches[0]);
        },
        
$line
    
);
    echo 
$line;
}
fclose($fp);
?>

subject

The string or an array with strings to search and replace.

limit

The maximum possible replacements for each pattern in each subject string. Defaults to -1 (no limit).

count

If specified, this variable will be filled with the number of replacements done.

Eerste pagina van Manuel PHP  Inhoudsopgave Haut

Return Values

preg_replace_callback() returns an array if the subject parameter is an array, or a string otherwise. On errors the return value is NULL

If matches are found, the new subject will be returned, otherwise subject will be returned unchanged.

Eerste pagina van Manuel PHP  Inhoudsopgave Haut

Changelog

Version Description
5.1.0 The count parameter was added

Eerste pagina van Manuel PHP  Inhoudsopgave Haut

Examples

Example #2 preg_replace_callback() example

<?php
// this text was used in 2002
// we want to get this up to date for 2003
$text "April fools day is 04/01/2002\n";
$text.= "Last christmas was 12/24/2001\n";
// the callback function
function next_year($matches)
{
  
// as usual: $matches[0] is the complete match
  // $matches[1] the match for the first subpattern
  // enclosed in '(...)' and so on
  
return $matches[1].($matches[2]+1);
}
echo 
preg_replace_callback(
            
"|(\d{2}/\d{2}/)(\d{4})|",
            
"next_year",
            
$text);

?>

The above example will output:

April fools day is 04/01/2003
Last christmas was 12/24/2002

Example #3 preg_replace_callback() using recursive structure to handle encapsulated BB code

<?php
$input 
"plain [indent] deep [indent] deeper [/indent] deep [/indent] plain";

function 
parseTagsRecursive($input)
{

    
$regex '#\[indent]((?:[^[]|\[(?!/?indent])|(?R))+)\[/indent]#';

    if (
is_array($input)) {
        
$input '<div style="margin-left: 10px">'.$input[1].'</div>';
    }

    return 
preg_replace_callback($regex'parseTagsRecursive'$input);
}

$output parseTagsRecursive($input);

echo 
$output;
?>

Eerste pagina van Manuel PHP  Inhoudsopgave Haut

See Also

Zoek een PHP-functie

Vertaling niet beschikbaar

De PHP-handleiding is nog niet in het Nederlands vertaald, dus het scherm is in het Engels. Als u wilt, kunt u het ook in het Frans of in het Duits raadplegen.

Als je de moed voelt, kun je je vertaling aanbieden ;-)

Nederlandse vertaling

U hebt gevraagd om deze site in het Nederlands te bezoeken. Voor nu wordt alleen de interface vertaald, maar nog niet alle inhoud.

Als je me wilt helpen met vertalingen, is je bijdrage welkom. Het enige dat u hoeft te doen, is u op de site registreren en mij een bericht sturen waarin u wordt gevraagd om u toe te voegen aan de groep vertalers, zodat u de gewenste pagina's kunt vertalen. Een link onderaan elke vertaalde pagina geeft aan dat u de vertaler bent en heeft een link naar uw profiel.

Bij voorbaat dank.

Document heeft de 30/01/2003 gemaakt, de laatste keer de 26/10/2018 gewijzigd
Bron van het afgedrukte document:https://www.gaudry.be/nl/php-rf-function.preg-replace-callback.html

De infobrol is een persoonlijke site waarvan de inhoud uitsluitend mijn verantwoordelijkheid is. De tekst is beschikbaar onder CreativeCommons-licentie (BY-NC-SA). Meer info op de gebruiksvoorwaarden en de auteur.

Referenties

  1. Bekijk - html-document Taal van het document:fr Manuel PHP : http://php.net

Deze verwijzingen en links verwijzen naar documenten die geraadpleegd zijn tijdens het schrijven van deze pagina, of die aanvullende informatie kunnen geven, maar de auteurs van deze bronnen kunnen niet verantwoordelijk worden gehouden voor de inhoud van deze pagina.
De auteur Deze site is als enige verantwoordelijk voor de manier waarop de verschillende concepten, en de vrijheden die met de referentiewerken worden genomen, hier worden gepresenteerd. Vergeet niet dat u meerdere broninformatie moet doorgeven om het risico op fouten te verkleinen.

Inhoudsopgave Haut