String Manipulation

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
darkelf
Forum Newbie
Posts: 6
Joined: Tue Sep 23, 2008 4:14 pm

String Manipulation

Post by darkelf »

Hello all.

I am new to PHP and I hope my question is simply a matter of overlooking the obvious. I have looked at many sites, including php.net, and I cannot find a way to elegantly manipulate a string (or array) in a manner I want.

I want to simply -- either through intrinsic or user-defined function -- update a value in a semi-colon-delimited string/array. For example, I would want to replace the sixth value in a string.

HAVE : 0;0;0;1;some_data;UNWANTED_DATA;0;0;1
WANT : 0;0;0;1;some_data;NEW_DATA;0;0;1

Does it require a loop to with a nested if statement? Is it a RegEx situation?

All help is appreciated.
marcth
Forum Contributor
Posts: 142
Joined: Mon Aug 25, 2008 8:16 am

Re: String Manipulation

Post by marcth »

Code: Select all

 
$string = '0;0;0;1;some_data;UNWANTED_DATA;0;0;1';
 
$stringParts = explode(';', $string);
$stringParts[5] = 'NEW_DATA';
 
$string = implode(';', $stingParts);
 
darkelf
Forum Newbie
Posts: 6
Joined: Tue Sep 23, 2008 4:14 pm

Re: String Manipulation

Post by darkelf »

Thank you.
Post Reply