Page 1 of 1

String Manipulation

Posted: Tue Sep 23, 2008 4:32 pm
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.

Re: String Manipulation

Posted: Tue Sep 23, 2008 6:00 pm
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);
 

Re: String Manipulation

Posted: Tue Sep 23, 2008 7:17 pm
by darkelf
Thank you.