How to remove part of a string

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
adsegzy
Forum Contributor
Posts: 184
Joined: Tue Jul 28, 2009 9:26 am

How to remove part of a string

Post by adsegzy »

Hello friends,

Pls how do i remove part of a string. eg

Code: Select all

$position = "2, 5, 7, 10, 15"; // $position is an array
$old = '5';
How do I remove $old from $position so that it will be like this.

Code: Select all

$new_position = "2, 7, 10, 15";
Thanks
adsegzy
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: How to remove part of a string

Post by Celauran »

You could use str_replace and replace the undesired bit with an empty string. You could explode into an array, unset the key, and implode back to a string. You could use preg_replace. There are a bunch of options.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to remove part of a string

Post by requinix »

Code: Select all

$position = "2, 5, 7, 10, 15"; // $position is an array
Uh, no?
Post Reply