bsamson Posted May 5, 2008 Share Posted May 5, 2008 Hello. I have the following script: <?php $depinfo = "01~01~111,02~02~222,03~03~333"; $depdays = explode(",", $depinfo); $nodays = count($depdays); for ($a=0; $a <= ($nodays-2); $a++) { $dayinfo = explode("~", $depdays[$a]); $depmo[$a] = $dayinfo[$a]; $depdy[$a] = $dayinfo[$a+1]; $depam[$a] = $dayinfo[$a+2]; echo $depmo[$a]."-".$depdy[$a]."-".$depam[$a]."<br>"; } ?> The output I am trying to achieve is: 01-01-111 02-02-222 03-03-333 The output I am getting is: 01-01-111 02-222- Can I please just get an idea where I am going wrong? Thanks in advance! Quote Link to comment https://forums.phpfreaks.com/topic/104241-string-manipulation/ Share on other sites More sharing options...
rhodesa Posted May 5, 2008 Share Posted May 5, 2008 <?php $depinfo = "01~01~111,02~02~222,03~03~333"; foreach(explode(",", $depinfo) as $item) { echo implode('-',explode("~",$item)).'<br>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/104241-string-manipulation/#findComment-533667 Share on other sites More sharing options...
rhodesa Posted May 5, 2008 Share Posted May 5, 2008 or... <?php $depinfo = "01~01~111,02~02~222,03~03~333"; echo str_replace('~','-',str_replace(',','<br>',$depinfo)); ?> Quote Link to comment https://forums.phpfreaks.com/topic/104241-string-manipulation/#findComment-533669 Share on other sites More sharing options...
bsamson Posted May 5, 2008 Author Share Posted May 5, 2008 Wow! I just have one more question. Oppose to echoing the results how would I go about storing the info in an array? Thanks for your help! Quote Link to comment https://forums.phpfreaks.com/topic/104241-string-manipulation/#findComment-533673 Share on other sites More sharing options...
flyhoney Posted May 5, 2008 Share Posted May 5, 2008 <?php $depinfo = "01~01~111,02~02~222,03~03~333"; $depdays = explode(",", $depinfo); $nodays = count($depdays); $depmo = array(); $depdy = array(); $depam = array(); foreach ($depdays as $day) { array_push($depmo, substr($day, 0, 2)); array_push($depdy, substr($day, 3, 2)); array_push($depam, substr($day, 6, 3)); echo substr($day, 0, 2)."-".substr($day, 3, 2)."-".substr($day, 6, 3)."<br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/104241-string-manipulation/#findComment-533674 Share on other sites More sharing options...
rhodesa Posted May 5, 2008 Share Posted May 5, 2008 array_push...really? people still use that? <?php $depinfo = "01~01~111,02~02~222,03~03~333"; $depmo = $depdy = $depam = array(); foreach (explode(",", $depinfo) as $day) { $depmo[] = substr($day, 0, 2); $depdy[] = substr($day, 3, 2); $depam[] = substr($day, 6, 3); echo substr($day, 0, 2)."-".substr($day, 3, 2)."-".substr($day, 6, 3)."<br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/104241-string-manipulation/#findComment-533679 Share on other sites More sharing options...
flyhoney Posted May 5, 2008 Share Posted May 5, 2008 Yikes, I've been writing Java too long. Quote Link to comment https://forums.phpfreaks.com/topic/104241-string-manipulation/#findComment-533684 Share on other sites More sharing options...
rhodesa Posted May 5, 2008 Share Posted May 5, 2008 Another note...i personally would use an array of associative arrays so it's all in one variable: <?php $depinfo = "01~01~111,02~02~222,03~03~333"; $deparray = array(); foreach (explode(",", $depinfo) as $day) { $item = array( 'mo' => substr($day, 0, 2), 'dy' => substr($day, 3, 2), 'am' => substr($day, 6, 3), ); echo "{$item['mo']}-{$item['dy']}-{$item['am']}<br>"; $deparray[] = $item; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/104241-string-manipulation/#findComment-533691 Share on other sites More sharing options...
bsamson Posted May 5, 2008 Author Share Posted May 5, 2008 Thanks for all the help so far! I was curious though. Why didn't my original example work? Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/104241-string-manipulation/#findComment-533732 Share on other sites More sharing options...
rhodesa Posted May 5, 2008 Share Posted May 5, 2008 this line: for ($a=0; $a <= ($nodays-2); $a++) { should have been for ($a=0; $a < $nodays; $a++) { and these: $depmo[$a] = $dayinfo[$a]; $depdy[$a] = $dayinfo[$a+1]; $depam[$a] = $dayinfo[$a+2]; should have been $depmo[$a] = $dayinfo[0]; $depdy[$a] = $dayinfo[1]; $depam[$a] = $dayinfo[2]; Quote Link to comment https://forums.phpfreaks.com/topic/104241-string-manipulation/#findComment-533737 Share on other sites More sharing options...
bsamson Posted May 5, 2008 Author Share Posted May 5, 2008 THANKS! Quote Link to comment https://forums.phpfreaks.com/topic/104241-string-manipulation/#findComment-533758 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.