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
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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.