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 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); } } 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. 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); 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é! Link to comment https://forums.phpfreaks.com/topic/211583-array/#findComment-1103181 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.