Jump to content

preg replace, number +1


ted_chou12

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/198417-preg-replace-number-1/
Share on other sites

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.