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;  
    } 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗