Forum Moderators: coopster

Message Too Old, No Replies

replace a particular character according to its occurrence

         

ayushchd

3:03 pm on Oct 8, 2007 (gmt 0)

10+ Year Member



Hi, suppose I have a variable $contents and it has something like this :

Liabilities Others Assets Others Income Others

I want to replace the first others with - liableothers, the secound others with assetothers and the last one with incomeothers

How can I do that?

PHP_Chimp

5:52 pm on Oct 8, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Would this be a continuation of the other problems?
As they started with you getting information out of an SQL database. If this is a continuation then use SQL to get the names you want i.e.

"SELECT some_thing AS \"Some Thing\", ....";

Another option is if $content is an array then you know that $content[1], $content[3] and $content[5] need to be changed, so assign your somethingothers to them i.e. $content[1] = 'liableothers';
This will only work assuming the array will stay content.

If $content is a string then you could explode the contents into an array then reassign them, as above, then implode them back again.

If the contents of $content are not constant then you could explode them. Then loop through each to find 'others', but you would then need a switch/if/elseif/else loop to go through all the possible combinations of the contents of the previous part of the array. i.e.


$len = count($content);
for ($i=0; $i<$len; $i++) {
if ($content[$i] == 'other' && $content[$i-1] == 'Liabilities') {
$content[$i] = 'liableothers';
}
else // continue looking for all combinations
}