What this does is that it takes the commas out of oldlist and put into variable newlist, then it will add $src[0] in the end of the list. The problem is here. If $newlist already contains $src[0] then it will be added again, I don't want that. Like martin,jessica,elvis and if $src[0] is martin as well, it should not add it to the end of the list. The code is already heavy, I need something light as possible. $newlist = str_replace(" ","",$oldlist); $newlist .= "$src[0]"; PHP:
You should use arrays instead of strings for this purpose. PHP has the function in_array which you can use then to check if an element already exists. Example: if (!in_array ($src[0], $oldlist)) { // do something } PHP:
Thanks man, it works perfectly. Now I'm down to one issue left $src[0] is the receiver. We already mad sure the receiver doesn't get on the list more than once. and $sender is the sender. I need to make sure the sender doesn't get on the list. I need to make sure $sender doesn't get on the $newlist EDIT: Sometimes the sender is ALREADY on the list, so I need to take him out, he can be in the middle of the list so I assume I need to take out the comma in front of the name too, otherwise there will be 2 commas. But the name can be the first one in the list and there might not be any commas at all. $oldlist .= "".$pid_cc.", "; $newlist= str_replace(" ","",$oldlist); if (!in_array($src[0], explode(',', $oldlist))) { $newlist.= $src[0]; } PHP:
$array = explode(',', $oldlist); if (!in_array($src[0], $array)) { $newlist.= $src[0]; } if (!in_array($sender, $array)) { $newlist.= $sender; }
Untested, but you get the hint: $oldlist .= "".$pid_cc.", "; $newlist= str_replace(" ","",$oldlist); $users = explode(',', $oldlist); if (($key = array_search($sender, $users)) !== false) { unset($users[$key]); } if (!in_array($src[0], $users)) { $users[] = $src[0]; } $newlist = implode(', ', $users); PHP:
OK, I messed with the code for 2 hours now. I couldn't get the second issue solved. I'm thinking here that maybe I'll leave that page like that and make the modification in another page that pulls the data from the database. This is how it pulls it (it posts it in a url like this) <? echo $sole['newlist']?> PHP: What I want to do is to remove $sender from the echoed $sole['newlist'] if it's there. NOTE: the name can appear in the beginning of the list: jack,jess,martin (has no comma) and it can appear in the middle or in the end of the list jess,jack,martin (has a comma in front of it) So if we want to remove jack ($sender] from the echoed $sole['newlist'] we would also have to remove the comma that is in front of the name because otherwise it would bug up the system. Any ideas?
Try this: $sole['newlist'] = str_replace($sender.",","",$sole['newlist']); $sole['newlist'] = str_replace(",".$sender,"",$sole['newlist']); PHP:
Thank you. Had to figure out a way to make str_replace in-case sensitive and found this str_ireplace(); Thanks again.