Walker33 Posted May 21, 2009 Share Posted May 21, 2009 I have a column that contains a string, for example, R01,R02,R03,R04,S01,S02,S03,A01,A02. I need to isolate the last R or S or A, which my code does, but then I need to add one to that final variable. ++ just repeats it. Is there a simple way to do this then to split up $result ? <?php $query6 = pg_query("SELECT sublicenses FROM sublicenses WHERE license = '3RF6EWOT2'"); $getarray = pg_fetch_assoc($query6); $license6 = $getarray['sublicenses']; echo "$license6 <br><br>"; $string="$license6"; $domain3=strrchr($string,"S"); echo "$domain3<br><br>"; if (!$domain3){echo "no licenses<br><br>";} $string2=split(",",$domain3); print_r ($string2); $result = $string2['0']; echo "<br><br>$result"; $addstn = $result++; echo "<br><br>$addstn"; ?> So my $result returns what I want, S03. But my $addstn also returns S03. I need a final return of S04, in this instance. Thanks a lot for any tips! Link to comment https://forums.phpfreaks.com/topic/159098-solved-adding-1-to-alphanumeric-value/ Share on other sites More sharing options...
gevans Posted May 21, 2009 Share Posted May 21, 2009 I'm not sure if this will, work, but off the top of my head; <?php //This works if there are always 3 characters $number = (int) $result[2]; $new_number = $number+1; $result = srr_replace($number, $new_number, $result); echo $result; Link to comment https://forums.phpfreaks.com/topic/159098-solved-adding-1-to-alphanumeric-value/#findComment-839038 Share on other sites More sharing options...
Walker33 Posted May 21, 2009 Author Share Posted May 21, 2009 Hmm. Put it in just as you wrote it, but not getting a return for the echo. Link to comment https://forums.phpfreaks.com/topic/159098-solved-adding-1-to-alphanumeric-value/#findComment-839046 Share on other sites More sharing options...
Walker33 Posted May 21, 2009 Author Share Posted May 21, 2009 And yes, it will always be a three-digit variable, one letter, two numbers. Link to comment https://forums.phpfreaks.com/topic/159098-solved-adding-1-to-alphanumeric-value/#findComment-839047 Share on other sites More sharing options...
gevans Posted May 21, 2009 Share Posted May 21, 2009 little syntax error; $result = srr_replace($number, $new_number, $result); should be $result = str_replace($number, $new_number, $result); Link to comment https://forums.phpfreaks.com/topic/159098-solved-adding-1-to-alphanumeric-value/#findComment-839056 Share on other sites More sharing options...
Walker33 Posted May 21, 2009 Author Share Posted May 21, 2009 Beautiful! Works like a champ. Thank you very much. I'm pretty new to this, so I wasn't aware of the str_replace function. Really appreciate your help! Link to comment https://forums.phpfreaks.com/topic/159098-solved-adding-1-to-alphanumeric-value/#findComment-839057 Share on other sites More sharing options...
Walker33 Posted May 21, 2009 Author Share Posted May 21, 2009 Spoke a second too soon. If my result is R11, your script returns R22 instead of R12. Link to comment https://forums.phpfreaks.com/topic/159098-solved-adding-1-to-alphanumeric-value/#findComment-839062 Share on other sites More sharing options...
gevans Posted May 21, 2009 Share Posted May 21, 2009 Try this <?php $result = substr_replace($result, $new_number, 2); Link to comment https://forums.phpfreaks.com/topic/159098-solved-adding-1-to-alphanumeric-value/#findComment-839071 Share on other sites More sharing options...
Walker33 Posted May 21, 2009 Author Share Posted May 21, 2009 That's the ticket! This time I tested all dup numbers, 11, 22, 33, etc. Perfect. Thanks again, your help is greatly appreciated. Link to comment https://forums.phpfreaks.com/topic/159098-solved-adding-1-to-alphanumeric-value/#findComment-839083 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.