Error trying to pass regex match to function

后端 未结 5 1625
生来不讨喜
生来不讨喜 2021-01-19 13:03

I\'m getting Syntax error, unexpected T_LNUMBER, expecting T_VARIABLE or \'$\'

This is the code i\'m using

function wpse44503_filter_con         


        
5条回答
  •  悲&欢浪女
    2021-01-19 13:35

    What you're trying to do (ie replacing the matched string with the result of a function call) can't be done using preg_replace, you'll need to use preg_replace_callback instead to get a function called for every match.

    A short example of preg_replace_callback;

    $get_site_url =                    // Returns replacement
      function($row) { 
        return '!'.$row[1].'!';        // row[1] is first "backref"
      };                                                     
    
    $str = 'olle';
    $regex = '/(ll)/';                 // String to match
    
    $output = preg_replace_callback(   // Match, calling get_site_url for replacement
        $regex,
        $get_site_url,
        $str);
    
    var_dump($output);                 // output "o!ll!e"
    

提交回复
热议问题