Jump to content


I'm stumped using str_replace()

  • Please log in to reply
3 replies to this topic

#1 Drummerbones117

  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 15 June 2006 - 03:25 PM

Alright im stumped on trying to figure this one out, maybe I shouldn't be using str_replace but it's the only thing I can think of right now.

I have a string of numbers seperated by commas like so "2,5,7,10,12,14," And I need to delete one of the numbers so I used str_replace and it works it's just that when I do it for the value 2 I end up deleting the 2 in the 12 also, anyone have any ideas?

Here's a sample of the code I have:
$id = '2';
$bla = '2,7,10,11,12,';
$bla = str_replace($id.',', "", $bla);
echo $bla;
///outputs 7,10,11,1

Any help would be greatly appreciated, thanks.

#2 .josh

  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 15 June 2006 - 03:43 PM

how about this ghetto code:
$id = '12';
$bla = '2,7,10,11,12';
echo $bla."<br>";
$blabla = explode(',', $bla);
foreach($blabla as $key => $val) {
   if($val == $id) {
$bla = implode(',',$blabla);
echo $bla;

Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#3 poirot

  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 15 June 2006 - 03:50 PM

This can be accomplished with:

$bla = str_replace(",$id," ",", $bla)

This would obviously force you to have commas at the beginning and end of the string.
~ D Kuang

#4 Drummerbones117

  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 15 June 2006 - 05:26 PM

Thanks Crayon Violent for the "ghetto" code :-P works great. Should've thought to explode it first but sometimes I guess when you look at something so long you end up overthinking.

Poirot I would have done that it's just a mess if I try and explode it.

Well thanks guys.

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users