Jump to content

String Manipulation ...


bsamson

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/104241-string-manipulation/
Share on other sites

<?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>"; 
}
?>

Link to comment
https://forums.phpfreaks.com/topic/104241-string-manipulation/#findComment-533674
Share on other sites

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>"; 
}
?>

Link to comment
https://forums.phpfreaks.com/topic/104241-string-manipulation/#findComment-533679
Share on other sites

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;
  }
?>

Link to comment
https://forums.phpfreaks.com/topic/104241-string-manipulation/#findComment-533691
Share on other sites

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];

Link to comment
https://forums.phpfreaks.com/topic/104241-string-manipulation/#findComment-533737
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.