PHP Strings into values

I have an array with some strings that I would like to extract values from.  The array looks like:

Array
(
    [0] => 09-29-15  11:31AM                55841 amensunset.jpg
    [1] => 09-08-15  01:55PM               260187 firstdayvpk.jpg
    [2] => 09-08-15  02:41PM            556373631 globe.psd
    [3] => 09-29-15  11:31AM               875215 keywestpkg.jpg
    [4] => 09-29-15  11:31AM                57599 revkeywestpkg.jpg
    [5] => 09-29-15  10:58AM               123836 revpizza.jpg
    [6] => 09-29-15  11:31AM                66106 revromancepkg.jpg
    [7] => 09-29-15  11:33AM               102603 revtouristpkg.jpg
    [8] => 09-29-15  11:31AM               310514 romancepkg.jpg
    [9] => 09-29-15  11:15AM               165873 SC1.jpg
    [10] => 09-29-15  11:32AM             12349206 Tarpon Creek 45.jpg
    [11] => 09-29-15  11:32AM             13813169 touristpkg.jpg
    [12] => 09-29-15  11:15AM                26340 wedding.jpg
)

Open in new window


I would like to be able to grab each value...  Date, Time, File Size and name.  How would I best go about doing that?  :)  Some sort of preg_match function?

Thanks in advance!
LVL 2
erzoolanderAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Julian HansenCommented:
Something like this using preg_match (http://php.net/manual/en/function.preg-match.php)
<?php
// SAMPLE DATA
$data = array
(
  '09-29-15  11:31AM                55841 amensunset.jpg',
  '09-08-15  01:55PM               260187 firstdayvpk.jpg',
  '09-08-15  02:41PM            556373631 globe.psd',
  '09-29-15  11:31AM               875215 keywestpkg.jpg',
  '09-29-15  11:31AM                57599 revkeywestpkg.jpg',
  '09-29-15  10:58AM               123836 revpizza.jpg',
  '09-29-15  11:31AM                66106 revromancepkg.jpg',
  '09-29-15  11:33AM               102603 revtouristpkg.jpg',
  '09-29-15  11:31AM               310514 romancepkg.jpg',
  '09-29-15  11:15AM               165873 SC1.jpg',
  '09-29-15  11:32AM             12349206 Tarpon Creek 45.jpg',
  '09-29-15  11:32AM             13813169 touristpkg.jpg',
  '09-29-15  11:15AM                26340 wedding.jpg',
);

// RESULT ARRAY
$parsed = array();

// LOOP THROUGH DATA
foreach($data as $d) {
  // REGULAR EXPRESSION MATCH
  $res = preg_match('/(\d\d\-\d\d\-\d\d)\s+ (\d\d:\d\d[A|P]M)\s+(\d+) (.*)/',$d, $matches);

  // IF MATCH FOUND
  if ($res) {
    // LOOSE THE  FIRST ITEM IN THE ARRAY - IT IS NOT NEEDED
    // REFER preg_match API PAGE FOR MORE INFO
    array_shift($matches);

    // ADD REMAINING ARRAY TO OUTPUT
    $parsed[] = $matches;
  }
}

echo "<pre>" . print_r($parsed, true) . "</pre>";

Open in new window

Ray PaseurCommented:
I'd probably do it this way.  
http://iconoun.com/demo/temp_errzoolander.php

The date and time columns should probably be treated as date/time values (a single value instead of two separate values) and formatted into the ISO-8601 standard format for internal representations of date/time values.  More on date/time handling here:
http://www.experts-exchange.com/articles/20920/Handling-Time-and-Date-in-PHP-and-MySQL-OOP-Version.html

<?php // demo/temp_errzoolander.php
/**
 * http://www.experts-exchange.com/questions/28734223/PHP-Strings-into-values.html
 *
 * Turn an array of string data into usable objects
 */
error_reporting(E_ALL);
echo '<pre>';


$data = array
(
  '09-29-15  11:31AM                55841 amensunset.jpg',
  '09-08-15  01:55PM               260187 firstdayvpk.jpg',
  '09-08-15  02:41PM            556373631 globe.psd',
  '09-29-15  11:31AM               875215 keywestpkg.jpg',
  '09-29-15  11:31AM                57599 revkeywestpkg.jpg',
  '09-29-15  10:58AM               123836 revpizza.jpg',
  '09-29-15  11:31AM                66106 revromancepkg.jpg',
  '09-29-15  11:33AM               102603 revtouristpkg.jpg',
  '09-29-15  11:31AM               310514 romancepkg.jpg',
  '09-29-15  11:15AM               165873 SC1.jpg',
  '09-29-15  11:32AM             12349206 Tarpon Creek 45.jpg',
  '09-29-15  11:32AM             13813169 touristpkg.jpg',
  '09-29-15  11:15AM                26340 wedding.jpg',
)
;

// A CLASS TO REPRESENT EACH FILE
Class FileData
{
    public $name, $size, $date, $time;
    public function __construct($str)
    {
        $str = preg_replace('/\s\s+/', ' ', $str);
        $arr = explode(' ', $str);
        $this->date = trim($arr[0]);
        $this->time = trim($arr[1]);
        $this->size = trim($arr[2]);
        $this->name = trim($arr[3]);
        return $this;
    }
}


// COLLECT THE ARRAY OF OBJECTS HERE
$new = [];
foreach ($data as $str)
{
    $obj = new FileData($str);
    $new[$obj->name] = $obj;
}

// SHOW THE TRANSFORMED DATA
print_r($new);

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
erzoolanderAuthor Commented:
Thanks!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.