ted_chou12 Posted April 13, 2010 Share Posted April 13, 2010 Hi, I am trying to replace each frame number with a larger number: $content = "<frame> [b]0[/b] .... pic: 0 state: 0 wait: 9 next: [b]1[/b] dvx: 0 dvy: 0 dvz: 0 ... <frame_end> <frame> [b]1[/b] ... pic: 1 state: 0 wait: 3 next: [b]2[/b] dvx: 0 dvy: 0 dvz: 0 centerx: 26 centery: 77 hit_a: 0 hit_d: 0 hit_j: 0 hit_Fa: 260 hit_Da: 240 .............. ................ <frame_end>"; $content = preg_replace("\\<frame\>\s(*.?)\s", "$1+1", $content); the line at the bottom is what I have tried so far, the number in bold is what I want to add 1 on to, ... represents any content. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/198417-preg-replace-number-1/ Share on other sites More sharing options...
Ken2k7 Posted April 13, 2010 Share Posted April 13, 2010 I think: preg_replace('#<frame>\s(\d+)(.+?) next:\s(\d+)#', "<frame> ($1+1)$2 next: ($3+1)", $content); Quote Link to comment https://forums.phpfreaks.com/topic/198417-preg-replace-number-1/#findComment-1041166 Share on other sites More sharing options...
salathe Posted April 13, 2010 Share Posted April 13, 2010 I would probably go for something like: function add_one($match) { return $match[0] + 1; } $content = preg_replace_callback('/(?:<frame>|next:) \K\d+/', 'add_one', $content); It looks for either <frame> or next: followed by a space, then empties what has been matched so far and matches a number. That number is then passed to the add_one function and its return value is what is used to substitute the original matched number. Quote Link to comment https://forums.phpfreaks.com/topic/198417-preg-replace-number-1/#findComment-1041174 Share on other sites More sharing options...
ted_chou12 Posted April 14, 2010 Author Share Posted April 14, 2010 Thank you! I got it, and I learnt a new function too. Quote Link to comment https://forums.phpfreaks.com/topic/198417-preg-replace-number-1/#findComment-1041352 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.