Jump to content

Replace and increment?


lfernando

Recommended Posts

Hi there!

Hoping someone can help me with this very specific issue :)

 

I have a string that looks like this:

var1
var2
X
var3
var4
X
var5

 

I need to find the highest number next to var (in this case 5), and to replace X with the following number (in this case var6).

So after the replace, the string would look like this:

 

var1
var2
var6 (this x has been replaced for var6)
var3
var4
var7 (this x has been replaced for var7)
var5

 

Any ideas!?

 

Thanks!

Link to comment
https://forums.phpfreaks.com/topic/211894-replace-and-increment/
Share on other sites

Hi there! Thank you so much for your quick reply!

This works great, except its picking up all numbers on the string. I only want to pick up the number after "var", so this string:

 

var3
var5
x
var6
something8
something9

 

should be replaced for

 

var3
var5
var7 (this x has been replaced for 7)
var6
something8
something9

 

 

Change this:

 

preg_match_all('/[\d]+/', $string, $matches);

To this:

preg_match_all('/var[\d]+/', $string, $matches);

 

And this:

 

$string = preg_replace('/X/e', '"var".++$number', $string);

To this:

$string = preg_replace('/X/e', '++$number', $string);

Thanks!! That's exactly what I needed.You made my week! :)

 

I'm using "var" in this example but in reality the number is inside of a dropdown list, like this:

"<br><select name=". $var.  $NUMBER . "_tab1 ><option value='blank' selected></option><option style='color:green;' style value=pass>Pass</option><option style='color:red;' value=fail>Fail</option><option style='color;gray;' value=na>N/A</option></select> "

 

Do you know how to incorporate this into your code?

 

Thank you again for all your help

 

 

Thanks!! That's exactly what I needed.You made my week! :)

 

I'm using "var" in this example but in reality the number is inside of a dropdown list, like this:

"<br><select name=". $var.  $NUMBER . "_tab1 ><option value='blank' selected></option><option style='color:green;' style value=pass>Pass</option><option style='color:red;' value=fail>Fail</option><option style='color;gray;' value=na>N/A</option></select> "

 

Do you know how to incorporate this into your code?

 

Thank you again for all your help

 

I have no idea what you're doing.  If you would explain what you want to accomplish at a higher level than searching and replacing, maybe it will be much easier than you thought.

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.