douzhi19900102 2012-02-04 17:48
浏览 29
已采纳

在preg_replace中应用第二或第三项的函数(preg_replace_callback?)

I have below a simple (BBCode) PHP code for insert code into a comment/post.

function highlight_code($str) {  
    $search = array( 
                   '/\[code=(.*?),(.*?)\](((?R)|.)*?)\[\/code\]/is',
                   '/\[quickcode=(.*?)\](((?R)|.)*?)\[\/quickcode\]/is'
                   );

    $replace = array(  
            '<pre title="$2" class="brush: $1;">$3</pre>',
            '<pre class="brush: $1; gutter: false;">$2</pre>'
            );  

    $str = preg_replace($search, $replace, $str);  
    return $str;  
}  

What I want to be able to do is insert functions at these locations:

    $replace = array(  
            '<pre title="$2" class="brush: $1;">'.myFunction('$3').'</pre>',
                                                       ^here
            '<pre class="brush: $1; gutter: false;">'.myFunction('$2').'</pre>'
                                                           ^here
            );  

From what I've read on SO I may need to use preg_replace_callback() or an e-modifier, but I cannot figure out how to go about doing this; my knowledge with regex is not so good. Would appreciate some help!

  • 写回答

1条回答 默认 最新

  • douquanzhan0315 2012-02-04 18:59
    关注

    You can either use this snippet (e-modifier):

    function highlight_code($str) {  
        $search = array( 
                       '/\[code=(.*?),(.*?)\](((?R)|.)*?)\[\/code\]/ise',
                       '/\[quickcode=(.*?)\](((?R)|.)*?)\[\/quickcode\]/ise'
                       );
        // these replacements will be passed to eval and executed. Note the escaped 
        // single quotes to get a string literal within the eval'd code    
        $replace = array(  
                '\'<pre title="$2" class="brush: $1;">\'.myFunction(\'$3\').\'</pre>\'',
                '\'<pre class="brush: $1; gutter: false;">\'.myFunction(\'$2\').\'</pre>\''
                );  
    
        $str = preg_replace($search, $replace, $str);  
        return $str;  
    } 
    

    or this one (Callback):

    function highlight_code($str) {  
        $search = array( 
                       '/\[code=(.*?),(.*?)\](((?R)|.)*?)\[\/code\]/is',
                       '/\[quickcode=(.*?)\](((?R)|.)*?)\[\/quickcode\]/is'
                       );
    
        // Array of PHP 5.3 Closures
        $replace = array(
                         function ($matches) {
                             return '<pre title="'.$matches[2].'" class="brush: '.$matches[1].';">'.myFunction($matches[3]).'</pre>';
                         },
                         function ($matches) {
                             return '<pre class="brush: '.$matches[1].'; gutter: false">'.myFunction($matches[2]).'</pre>';
                         }
                );  
        // preg_replace_callback does not support array replacements.
        foreach ($search as $key => $regex) {
            $str = preg_replace_callback($search[$key], $replace[$key], $str);
        }
        return $str;  
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动