0

I have been trying to explode a string twice then imploding it. Here's what I mean by exploding it twice. So I have a string

:0,0,1,0,0:0,0,0,0,0:

I want to edit the third zero and change it to one. How do I do that? I"ve tried exploding the : then exploding the , but then Idk how to implode it.

I have tried this code but I can't implode the :

$exploding = ':0,0,0,1,0,0:0,0,0,0,0:';
 $explode = explode(':', $exploding);
 $explodes = explode(',', $explode[1]);
 $explodes[$part] = $type;
 $explodes = implode(',', $explodes);
asked Aug 23, 2014 at 7:43
2

4 Answers 4

1

I added another answer specifically answering the question asked, but might I suggest a different approach that is more extensible to more complex problems? PHP allows you to access strings as arrays, with the index being the zero-based position of a character in the string.

Code:

$string = ':sd0,2[wsdsjds0sdfs0ksdjse00df0';
echo $string.'<br />';
$zeroCounter = 0;
for ($i=0;$i<strlen($string);$i++) {
 if ($string[$i] === '0') {
 $zeroCounter += 1;
 if ($zeroCounter === 3) {
 $string[$i] = '1';
 break;
 }
 }
}
echo $string;

Output:

:sd0,2[wsdsjds0sdfs**0**ksdjse00df0
:sd0,2[wsdsjds0sdfs**1**ksdjse00df0

Let me know if you have any questions!

answered Aug 23, 2014 at 8:04
Sign up to request clarification or add additional context in comments.

Comments

0

$part and $type are undefined in this example, but this code should change the third 0 to a 1 for this specific string:

$exploding = ':0,0,0,1,0,0:0,0,0,0,0:';
 $explode = explode(':', $exploding);
 $explodes = explode(',', $explode[1]);
 $explodes[2] = '1';
 $explodes = implode(',', $explodes);

This seems oddly specific though, so I'm not sure if you're just trying to figure out how explode works or if you want something that will work with different strings as well.

Check my other solution for a better way of solving these problems.

answered Aug 23, 2014 at 7:52

Comments

0
// Explode on :
$array = explode(':', $string);
// Now explode each on ,
foreach ($array as &$substring) {
 $substring = explode(',', $substring);
}
// Change third 0 to 1
$array[1][3] = '1';
// Now implode on ,
foreach ($array as &$substring) {
 $substring = implode(',', $substring);
}
// Now implode on :
$new_string = implode(':', $array);

The reference variables in the foreach loops allow you to update the array elements in place.

DEMO

A simpler version that's like your attemp would be:

$exploding = ':0,0,0,1,0,0:0,0,0,0,0:';
$explode = explode(':', $exploding);
$explodes = explode(',', $explode[1]);
$explodes[$part] = $type;
$explode[1] = implode(',', $explodes);
$exploding = implode(':', $explode);

DEMO

answered Aug 23, 2014 at 7:49

1 Comment

instead of making it 1, it made it 01 and it removed the comma after it.
0

your code is correct but incomplete, this works for me:

$part = 3;
$type = 1;
$exploding = ':0,0,1,0,0,0:0,0,0,0,0:';
$explode = explode(':', $exploding);
$explode[1] = explode(',', $explode[1]);
$explode[1][$part] = $type;
$explode[1] = implode(',', $explode[1]);
$explode = implode(':', $explode);
print $exploding;
print "<br/>";
print $explode;

output:

:0,0,1,0,0,0:0,0,0,0,0:
:0,0,1,1,0,0:0,0,0,0,0:
answered Aug 23, 2014 at 8:17

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.