lfernando Posted August 27, 2010 Share Posted August 27, 2010 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! Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted August 27, 2010 Share Posted August 27, 2010 Probably a better way, but just off the top of my head: preg_match_all('/[\d]+/', $string, $matches); sort($matches[0]); $number = array_pop($matches[0]); $string = preg_replace('/X/e', '"var".++$number', $string); Quote Link to comment Share on other sites More sharing options...
lfernando Posted August 27, 2010 Author Share Posted August 27, 2010 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 Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted August 27, 2010 Share Posted August 27, 2010 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); Quote Link to comment Share on other sites More sharing options...
lfernando Posted August 27, 2010 Author Share Posted August 27, 2010 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 Quote Link to comment Share on other sites More sharing options...
AbraCadaver Posted August 27, 2010 Share Posted August 27, 2010 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. Quote Link to comment 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.