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 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥15 stable diffusion
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件