How to make preg_replace have count function for number of replaces count?
I have creted a preg_replace script but now i want to add count fu开发者_如何转开发nction in it!
MY CODE
$replace = 'ISO Burning Programs/Active@ ISO Burner 2.1.0.0/SPTDinst-v162-x86.exe';
$result=preg_replace('/[^0-9^A-Z^a-z-*… ,;_!@.{}#<>""=-^:()\[\]]/', '<br/>', $replace);
echo $result;
OUTPUT
ISO Burning Programs
Active@ ISO Burner 2.1.0.0
SPTDinst-v162-x86.exe
But the output i want is-
1 ISO Burning Programs
2 Active@ ISO Burner 2.1.0.0
3 SPTDinst-v162-x86.exe
Can anyone help me out?
Thanks in advance!!!!!!Alternatively you could just do this:
$replace = 'ISO Burning Programs/Active@ ISO Burner 2.1.0.0/SPTDinst-v162-x86.exe';
$result = explode('/', $replace);
foreach($result as $i => $value)
printf("%d %s<br />", ++$i, $value);
If you want to do this with preg
, you'd have to use preg_replace_callback:
$result = preg_replace_callback('/([^\/]*)(\/|$)/', function($matches){
static $count = 0;
$count++;
return !empty($matches[1]) ? $count.' '.$matches[1].'<br/>' : '';
}, $replace);
preg_replace has a count function in it, the -1 is limit (unlimited) and $count is the number of replacement. just FYI.
$result=preg_replace('/[^0-9^A-Z^a-z-*… ,;_!@.{}#<>""=-^:()\[\]]/', '<br/>\n', $replace, -1, $count);
$a = explode("\n", $result);
$i=1;
foreach($a as $res)
{
echo $i . " " . $res;
$i++;
}
精彩评论