PHP Strings into values

erzoolander
erzoolander used Ask the Experts™
on
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!
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Most Valuable Expert 2017
Distinguished Expert 2018
Commented:
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

Most Valuable Expert 2011
Top Expert 2016
Commented:
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

Author

Commented:
Thanks!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial