I have the regex to find all numbers with percentage (100|\d\d|\d)\% - this regex is given and I am not allowed to change it. I have input string:
Lorem ipsum 10% dolor, 8%sit amet 50%
I need then to highligh (with HTML tags) parts of string according to that regex so I'd get
Lorem ipsum <i>10%</i> dolor, <i>8%</i>sit amet <i>50%</i>
Because preg_replace would work only with regex like (100%|\d\d%|\d%) otherwise it would highlight only numbers 0-100. I am using preg_replace_callback like this:
<?php
$string = "Lorem ipsum 10% dolor, 8%sit amet 50%";
$count = 0;
function foo($matches){
global $count;
return "<i>".$matches[$count++]."</i>";
}
$reg = "/(100|\d\d|\d)\%/";
$str_out = preg_replace_callback($reg, "foo", $string, PREG_PATTERN_ORDER);
?>
The output is this though:
Lorem ipsum <i>10%</i> dolor, 8%sit amet 50%
Only first occurence is replaced.