unidox Posted August 24, 2010 Share Posted August 24, 2010 I have an array such as: Array ( [0] => : [1] => 353 [2] => SGS|Bot-1 [3] => = [4] => #a [5] => :SGS|Bot-1 [6] => +SGS|Bot [7] => SGS|Ryan`AFK [8] => @a|TaCo [9] => SGS|Pat [10] => @U|Pat [11] => @ChanServ ) How do I go through that array, and remove a "+" or a "@" from each index only if it has one in the beginning. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/211583-array/ Share on other sites More sharing options...
Alex Posted August 24, 2010 Share Posted August 24, 2010 foreach($arr as &$val) { if(substr($val, 0, 1) == "+" || substr($val, 0, 1) == "@") { $val = substr($val, 1); } } Quote Link to comment https://forums.phpfreaks.com/topic/211583-array/#findComment-1103010 Share on other sites More sharing options...
Adam Posted August 24, 2010 Share Posted August 24, 2010 If you have any knowledge of regex you may be comfortable with a solution like: foreach ($array as &$value) { $value = preg_replace('/^(@|\+)/', '', $value); } The characters in red are where you'd need to expand the code (with a pipe character delimiter between them) if you wanted to expand it to remove more characters in future -- the plus is a special character so it must be escaped first with a backslash. Quote Link to comment https://forums.phpfreaks.com/topic/211583-array/#findComment-1103027 Share on other sites More sharing options...
AbraCadaver Posted August 24, 2010 Share Posted August 24, 2010 If you have any knowledge of regex you may be comfortable with a solution like: foreach ($array as &$value) { $value = preg_replace('/^(@|\+)/', '', $value); } The characters in red are where you'd need to expand the code (with a pipe character delimiter between them) if you wanted to expand it to remove more characters in future -- the plus is a special character so it must be escaped first with a backslash. Why not just? $array = preg_replace('/^(@|\+)/', '', $array); Quote Link to comment https://forums.phpfreaks.com/topic/211583-array/#findComment-1103172 Share on other sites More sharing options...
Adam Posted August 24, 2010 Share Posted August 24, 2010 Why not just? $array = preg_replace('/^(@|\+)/', '', $array); Touché! Quote Link to comment https://forums.phpfreaks.com/topic/211583-array/#findComment-1103181 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.