Help with a simple function

Discussion in 'PHP' started by x0x, Aug 25, 2008.

  1. #1
    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:
     
    x0x, Aug 25, 2008 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    if (!in_array($src[0], explode(',', $oldlist)))
    {
        $newlist .= $src[0];
    }
    
    PHP:
     
    nico_swd, Aug 25, 2008 IP
  3. nulldev

    nulldev Peon

    Messages:
    3
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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:
     
    nulldev, Aug 25, 2008 IP
  4. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #4
    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:
     
    x0x, Aug 25, 2008 IP
  5. grandpa

    grandpa Active Member

    Messages:
    185
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    75
    #5
    $array = explode(',', $oldlist);
    if (!in_array($src[0], $array))
    {
    $newlist.= $src[0];
    }
    if (!in_array($sender, $array))
    {
    $newlist.= $sender;
    }
     
    grandpa, Aug 25, 2008 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    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:
     
    nico_swd, Aug 25, 2008 IP
  7. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #7
    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?
     
    x0x, Aug 25, 2008 IP
  8. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #8
    Anyone.........?
     
    x0x, Aug 25, 2008 IP
  9. logylaps

    logylaps Active Member

    Messages:
    761
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    85
    #9
    Try this:

    $sole['newlist'] = str_replace($sender.",","",$sole['newlist']);
    $sole['newlist'] = str_replace(",".$sender,"",$sole['newlist']);
    PHP:
     
    logylaps, Aug 25, 2008 IP
  10. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #10
    Thank you. Had to figure out a way to make str_replace in-case sensitive and found this str_ireplace();
    Thanks again.
     
    x0x, Aug 25, 2008 IP