preg_replace问题求解
我又一个字符串:
$html = '
<?php$html = '<ul><li>《mainGood》《mainGood》</li><li>《subGood》《subGood》《subGood》</li></ul>';echo preg_replace("/(《mainGood》|《subGood》)/eis","foo('$1')",$html);function foo($v){ global $count; if($v=='《mainGood》'){ return 1; }else{ $count ; return $count 1; }}//<ul><li>11</li><li>234</li></ul><br><font color="#e78608">------解决方案--------------------</font><br>$num_mainGood = $num_subGood = 0;<br>echo preg_replace_callback("/(《mainGood》)|(《subGood》)/is", '回调函数', $html);<br><br>function 回调函数($r) {<br> global $num_mainGood, $num_subGood;<br> if($r[1]) $num_mainGood ;<br> if($r[2]) $num_subGood ;<br> //以下写返回内容的代码<br><br>}<br><br><font color="#e78608">------解决方案--------------------</font><br>
[[email protected] php_project]$ php main.php <ul><li>a</li><li>bc</li></ul>[[email protected] php_project]$ cat main.php <?php$html = <<<EOF<ul><li>《mainGood》</li><li>《subGood》《subGood》</li></ul>EOF;$count = 0;$map = array(0 => 'a', 1 => 'b', 2 => 'c');$result = preg_replace_callback('/《mainGood》|《subGood》/i', function($match) { global $count; global $map; return $map[$count ]; }, $html);echo $result . PHP_EOL;?><div class="clear"></div>