Take the tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

i need something like that:

function highlight_code($str) {  
$search = array( 
               '/regexp1/i',
               '/regexp2/i',
               '/regexp3/i'
               );

// Array of PHP 5.3 Closures
$replace = array(
                 myFunction1($matches),
                 myFunction2($matches),
                 myFunction3($matches)
        );  
// preg_replace_callback does not support array replacements.
foreach ($search as $key => $regex) {
    $str = preg_replace_callback($search[$key], $replace[$key], $str);
}
return $str;  
} 

but running preg_replace_callback only 1 time since my $str is a large document and can be slow way looping this many times.

i cannot use preg_replace since i need to pass what has been matched by patterns to a function that must be process it.

i've read this article that seems to be a solution: preg_replace with multiple patterns replacements at once

but i didn't understand very well how grouping works and how can i use it.. moreover i would keep the array way instead long regexp string ( if they do not change performances ) since they are more clear.

could you help me? thanks in advance

share|improve this question
 
Have you tried preg_replace with the /e flag for instead of the anonymous functions? Looking at your input it would have crazy bad security though. –  complex857 Apr 5 at 20:52
1  
@complex857 - No. See the manual on /e: This feature has been DEPRECATED as of PHP 5.5.0. Relying on this feature is highly discouraged. –  nickb Apr 5 at 20:55
 
Ooh, thank you for your question, it's my introduction to (?R). –  Wrikken Apr 5 at 21:41
add comment

closed as not a real question by Dan Lugg, tereško, ircmaxell, Anthon, Graviton Apr 6 at 8:55

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

You can save some iterations by making your [tagname] unimportant / switch on it's contents:

$callback = function($matches){
    switch($matches[1]){
        case 'code':
                $data = explode(',',$matches[2]);
                return '<pre title="'.$data[0].'" class="brush: '.$data[1].';">'.myFunction($matches[3]).'</pre>';
                break;
        case 'quickcode':
                return '<pre class="brush: '.$matches[2].'; gutter: false">'.myFunction($matches[3]).'</pre>';
    }
};
while($check!=($string=preg_replace_callback(
        '#\[([a-z]+)(?:=(.*?))?\](((?R)|.)*?)\[/\1]#six',
        $callback,
        $string))){
   $check=$string;
}
share|improve this answer
 
sorry but i need a more generic way to solve it, patterns are not always the same ...i've just edited the example in first post to explain you what i mean –  Joseph Apr 5 at 22:21
1  
Well. first of all I'd thoroughly examine your recursion patterns then... Those are really expensive, so if avoidable, avoid (if not, well, expensive). And keep in mind you can nest the callbacks: if you think your [whatever ..] [/whatever] tags are to different from each other, just throw a new preg_replace on it in the callback. If it still takes to long... you might want to try to write a parser instead, which would be more elaborate, but would have the advantage of having to traverse the string once only. –  Wrikken Apr 5 at 22:36
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.