Sequential counting with preg_replace
I'm trying to do some seuqential counting thru a text file full of lines with preg_replace in PHP... While the code below works for me... It's counting in reverse... Anyone know how I can get the results to start from 1 instead of counting down to one???
$num = count($lines);
$return = preg_replace('%\$postnum%msi','post' . $num, $return);
It returns:
post10
post9
post8
post7
post6
post5
post4
开发者_StackOverflowpost3
post2
post1
I want it to return this:
post1
post2
post3
post4
post5
post6
post7
post8
post9
post10
Any suggestions?? Thx.
Have you considered array_reverse()?
$return = array_reverse(preg_replace('%\$postnum%msi','post' . $num, $return));
Generic example using the output from the question:
http://codepad.org/VYQ9VTwM
$return = array('post10',
'post9',
'post8',
'post7',
'post6',
'post5',
'post4',
'post3',
'post2',
'post1');
$result = array_reverse($return);
demo
It could help
精彩评论