Walker33 Posted June 23, 2009 Share Posted June 23, 2009 Hello again! Not sure I'm even going about this in the right way. Trying to delete characters from a string. String could be any length, characters could be anywhere, characters will always be 3 digits, followed by a comma. My code deletes everything following the variable, but I only want the variable $final3 deleted. <?php $final3 = 'R07'; $revokedrow = 'R11,S12,S13,A05,R07,A07,A10,S03'; $start = strpos($revokedrow, $final3); $newrevoked = substr_replace($revokedrow, '', $start, $start+2); echo "this is the new revoked line: $newrevoked<br>"; //getting R11,S12,S13,A05, instead of what I want, which is R11,S12,S13,A05,A07,A10,S03 ?> Any help is, as always, greatly appreciated! Quote Link to comment https://forums.phpfreaks.com/topic/163400-solved-substr_replace-question/ Share on other sites More sharing options...
flyhoney Posted June 23, 2009 Share Posted June 23, 2009 <?php $final3 = 'R07'; $revokedrow = 'R11,S12,S13,A05,R07,A07,A10,S03'; $codes = explode(',', $revokedrow); // see if $final3 is supposed to be revoked for ($x = 0; $x < count($codes); $x++) { if ($codes[$x] == $final3) { unset($codes[$x]); } } $newrevoked = implode(',', $codes); echo "this is the new revoked line: $newrevoked<br>"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/163400-solved-substr_replace-question/#findComment-862144 Share on other sites More sharing options...
mattal999 Posted June 23, 2009 Share Posted June 23, 2009 Couldn't you just do: <?php $final3 = 'R07'; $revokedrow = 'R11,S12,S13,A05,R07,A07,A10,S03'; $newrow = str_replace($final3.",", "", $revokedrow); ?> Quote Link to comment https://forums.phpfreaks.com/topic/163400-solved-substr_replace-question/#findComment-862148 Share on other sites More sharing options...
Walker33 Posted June 23, 2009 Author Share Posted June 23, 2009 Thanks! both worked. 2nd one seems simpler. Is there something else to consider in using mattal's instead of flyhoney's? Something I may not be seeing? Really appreciate the help. Quote Link to comment https://forums.phpfreaks.com/topic/163400-solved-substr_replace-question/#findComment-862160 Share on other sites More sharing options...
Alex Posted June 23, 2009 Share Posted June 23, 2009 Thanks! both worked. 2nd one seems simpler. Is there something else to consider in using mattal's instead of flyhoney's? Something I may not be seeing? Really appreciate the help. Efficiency, the second example is easier, so I'd go with that one. Quote Link to comment https://forums.phpfreaks.com/topic/163400-solved-substr_replace-question/#findComment-862162 Share on other sites More sharing options...
Walker33 Posted June 23, 2009 Author Share Posted June 23, 2009 Sorry, one thing I didn't see right. Your code(s) work fine so long as $final3 doesn't end the string, where there is no comma. Reversing ."," to the front of $final3, of course, works fine unless $final3 occurs at the beginning of the string. Any ideas? <?php $final3 = 'R07'; $revokedrow = 'R11,S12,S13,A05,R07,A07,A10,S03'; $newrow = str_replace($final3.",", "", $revokedrow); ?> Quote Link to comment https://forums.phpfreaks.com/topic/163400-solved-substr_replace-question/#findComment-862173 Share on other sites More sharing options...
mattal999 Posted June 23, 2009 Share Posted June 23, 2009 <?php $final3 = 'R07'; $revokedrow = 'R11,S12,S13,A05,R07,A07,A10,S03'; $newrow = str_replace($final3.",", "", $revokedrow); if($newrow == $revokedrow) { // Try a str replace without the comma. $newrow = str_replace($final3, "", $revokedrow); } if($newrow == $revokedrow) { // Is not in string. } ?> Quote Link to comment https://forums.phpfreaks.com/topic/163400-solved-substr_replace-question/#findComment-862178 Share on other sites More sharing options...
Walker33 Posted June 23, 2009 Author Share Posted June 23, 2009 Yep, that makes sense....probably should have thought of that. Thank you! You were a ton of help! Quote Link to comment https://forums.phpfreaks.com/topic/163400-solved-substr_replace-question/#findComment-862185 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.