preg_replace_callback

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

preg_replace_callback执锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷式锟斤拷锟斤拷锟斤拷锟斤拷使锟斤拷一锟斤拷锟截碉拷锟斤拷锟斤拷锟芥换

说锟斤拷

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

锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟轿拷锟斤拷锟� 锟斤拷锟斤拷指锟斤拷一锟斤拷 callback 锟斤拷锟� replacement 锟斤拷锟斤拷锟芥换 锟街凤拷锟斤拷锟侥硷拷锟姐,锟斤拷锟斤拷锟斤拷锟斤拷锟酵拷锟� preg_replace()锟斤拷

锟斤拷锟斤拷

pattern

要锟斤拷锟斤拷锟斤拷模式锟斤拷锟斤拷锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷一锟斤拷锟街凤拷锟斤拷锟斤拷锟介。

callback

一锟斤拷锟截碉拷锟斤拷锟斤拷锟斤拷锟斤拷每锟斤拷锟斤拷要锟芥换时锟斤拷锟矫o拷锟斤拷锟斤拷时锟斤拷锟斤拷锟矫碉拷锟侥诧拷锟斤拷锟角达拷subject 锟斤拷匹锟戒到锟侥斤拷锟斤拷锟斤拷氐锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷婊伙拷锟斤拷址锟斤拷锟斤拷锟斤拷锟斤拷歉没氐锟斤拷锟斤拷锟斤拷锟角╋拷锟斤拷锟�

handler ( array $matches ) : string

锟斤拷锟斤拷芫锟斤拷锟斤拷锟斤拷锟揭�callback锟斤拷锟斤拷锟斤拷 锟斤拷锟斤拷锟斤拷preg_replace_callback()一锟斤拷锟截凤拷锟侥碉拷锟矫★拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷拢锟斤拷锟斤拷锟斤拷 使锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷 为preg_replace_callback()锟斤拷锟斤拷时锟侥回碉拷锟斤拷 锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷员锟斤拷锟斤拷锟斤拷锟� 锟斤拷锟斤拷锟斤拷息锟斤拷同一锟斤拷位锟矫诧拷锟揭诧拷锟斤拷锟斤拷为一锟斤拷锟斤拷锟斤拷锟轿猴拷锟斤拷锟斤拷锟截凤拷使锟矫的回碉拷锟斤拷锟斤拷锟斤拷锟狡讹拷锟斤拷染锟斤拷锟斤拷锟斤拷锟狡空间。

Example #1 preg_replace_callback() 锟斤拷 锟斤拷锟斤拷锟斤拷锟斤拷

<?php
/* 一锟斤拷unix锟斤拷式锟斤拷锟斤拷锟斤拷锟叫癸拷锟斤拷锟斤拷锟斤拷锟斤拷锟节斤拷锟斤拷锟戒开始锟斤拷锟街的达拷写锟斤拷母转锟斤拷为小写锟斤拷 */
$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

要锟斤拷锟斤拷锟芥换锟斤拷目锟斤拷锟街凤拷锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷锟介。

limit

锟斤拷锟斤拷每锟斤拷模式锟斤拷锟斤拷每锟斤拷 subject 锟街凤拷锟斤拷锟斤拷锟斤拷锟斤拷锟芥换锟斤拷锟斤拷锟斤拷 默锟斤拷锟斤拷-1锟斤拷锟斤拷锟斤拷锟狡o拷锟斤拷

count

锟斤拷锟街革拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟轿拷婊恢达拷械拇锟斤拷锟斤拷锟�

锟斤拷锟斤拷值

锟斤拷锟�subject锟斤拷一锟斤拷锟斤拷锟介, preg_replace_callback()锟斤拷锟斤拷一锟斤拷锟斤拷锟介,锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷址锟斤拷锟斤拷锟� 锟斤拷锟斤拷锟斤拷时锟斤拷锟斤拷 NULL锟斤拷

锟斤拷锟斤拷锟斤拷业锟斤拷锟狡ワ拷洌拷锟斤拷锟斤拷婊伙拷锟斤拷目锟斤拷锟街凤拷锟斤拷锟斤拷锟斤拷锟街凤拷锟斤拷锟斤拷锟介)锟斤拷 锟斤拷锟斤拷锟斤拷锟�subject 锟斤拷锟斤拷锟睫变化锟斤拷锟截★拷

锟斤拷锟斤拷锟斤拷志

锟芥本 说锟斤拷
5.1.0 锟斤拷锟斤拷锟剿诧拷锟斤拷count锟斤拷

锟斤拷锟斤拷

Example #2 preg_replace_callback()示锟斤拷

<?php
// 锟斤拷锟侥憋拷锟叫碉拷锟斤拷锟斤拷锟斤拷锟揭伙拷锟�.
$text "April fools day is 04/01/2002\n";
$text.= "Last christmas was 12/24/2001\n";
// 锟截碉拷锟斤拷锟斤拷
function next_year($matches)
{
  
// 通锟斤拷: $matches[0]锟斤拷锟斤拷傻锟狡ワ拷锟�
  // $matches[1]锟角碉拷一锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷锟狡ワ拷锟�
  // 锟皆达拷锟斤拷锟斤拷
  
return $matches[1].($matches[2]+1);
}
echo 
preg_replace_callback(
            
"|(\d{2}/\d{2}/)(\d{4})|",
            
"next_year",
            
$text);

?>

锟斤拷锟斤拷锟斤拷锟教伙拷锟斤拷锟斤拷锟�

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

Example #3 preg_replace_callback()使锟矫递归构锟届处锟斤拷BB锟斤拷姆锟阶�

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

function 
parseTagsRecursive($input)
{
     
/* 锟斤拷注: 锟皆达拷锟斤拷锟斤拷锟斤拷式锟街段凤拷锟斤拷
     * 锟斤拷尾锟斤拷锟斤拷#锟斤拷锟斤拷锟斤拷指锟斤拷锟�
     * \[indent] 匹锟斤拷一锟斤拷原锟侥碉拷[indent]
     * ((?:[^[]|\[(?!/?indent])|(?R))+)锟斤拷锟斤拷:
     *   (?:[^[]|\[(?!/?indent])锟斤拷锟斤拷:
     *  锟斤拷锟斤拷锟斤拷锟斤拷一锟斤拷锟角诧拷锟斤拷锟斤拷锟斤拷
     *   锟斤拷锟斤拷锟斤拷选路锟斤拷, 一锟斤拷锟角凤拷[锟街凤拷, 锟斤拷一锟斤拷锟斤拷[锟街凤拷锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷挪锟斤拷锟�/indent锟斤拷indent.
     *   (?R) 锟斤拷锟斤拷锟斤拷式锟捷癸拷
     *     \[/indent] 匹锟斤拷锟斤拷锟斤拷锟絒/indent]
     * /

    $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;
?>

锟轿硷拷