Page 1 of 1

From text file to array

Posted: Thu Apr 28, 2005 4:25 pm
by Todd_Z
How, using regex, could i turn

Code: Select all

The Bullet Wives - Official Site - http://www.themiamovie.com/
Beat Kids - Official Site - http://www.beatkids.com/
Initial D - Official Site - http://www.initialdthemovie.com/
which is stored in links.txt into the results of:

Code: Select all

$links = array();

  $links[0] = array();
    $links[0]["Movie"] = "The Bullet Wives";
    $links[0]["Caption"] = "Official Site";
    $links[0]["Link"] = "http://www.themiamovie.com/";

  $links[1] = array();
    $links[1]["Movie"] = "The Bullet Wives";
    $links[1]["Caption"] = "Beat Kids";
    $links[1]["Link"] = "http://www.beatkids.com/";

  $links[2] = array();
    $links[2]["Movie"] = "Initial D";
    $links[2]["Caption"] = "Official Site";
    $links[2]["Link"] = "http://www.initialdthemovie.com/";
What I have so far:

Code: Select all

$file = file_get_contents( "../Links.txt" );
  $pattern = "/\n/";
  $links = preg_split( $pattern, $file );
  foreach ( $links as $link ) {
    list( $movie, $caption, $link ) = preg_split( "/ - /", $link );
    echo "<br>$movie: ($caption) $link";
  }

Posted: Thu Apr 28, 2005 4:56 pm
by McGruff
How about a file iterator. That will spit out one line at a time, simplifying the regex problem. In fact that might eliminate the need for regex altogether.

Next step would be to decorate the file iterator with a method which processes each line into an array. The essence of this could be:

Code: Select all

explode(' - ', $line)
.. if you can reliably separate data chunks on ' - '.

Posted: Thu Apr 28, 2005 6:44 pm
by nickvd
McGruff wrote:How about a file iterator. That will spit out one line at a time, simplifying the regex problem. In fact that might eliminate the need for regex altogether.

Next step would be to decorate the file iterator with a method which processes each line into an array. The essence of this could be:

Code: Select all

explode(' - ', $line)
.. if you can reliably separate data chunks on ' - '.
Precicely what I would suggest

Code: Select all

$file = file(&quote;../Links.txt&quote;);
foreach ($file as $line) {
   $links&#1111;] = explode(&quote; - &quote;, $line);
}
Although, that would fail if the data was like:

Code: Select all

The Bul - let Wives - Official Site - http://www.themiamovie.com/
Beat Kids - Official Site - http://www.beatkids.com/
Initial D - Official Site - http://www.initialdthemovie.com/
but if you are 100% sure that you can rely on "<space><dash><space>" than you should be okay...

re

Posted: Thu Apr 28, 2005 7:59 pm
by harrisonad

Code: Select all

// initialize
$aray = array(); 
$count = 0;
// set the array keys to be used
$keys = array('movie','caption','link');
// do the file thing...
$file = file("../Links.txt");
// loop for every line
foreach ($file as $line) {
   // convert line string to array
   $details = explode('-',$line);
   // loop 3 times
   for($x=0;$x<3;$x++){
     // use the value of $x to set the key and the values
     // so that $keys[0] which is 'movies' will have
     // a value of $details[0] which is 'The Bullet Wives' 
     // and so on...
     $aray[++$count][$keys[$x]] = $details[$x];
   }
}

Posted: Thu Apr 28, 2005 9:01 pm
by McGruff
Although you can loop through a file one line at a time using a file() array, you have to read the whole (multi-megabyte?) file into memory. With an iterator, you're only reading one line at a time.

Posted: Fri Apr 29, 2005 1:07 pm
by Skara
or you could do something like this:

Code: Select all

$data = file_get_contents('file.txt');
$first = explode("\n",$data);
for ($i=0; $i < count($first); $i++)
  $final[$i] = explode(' - ',$first[$i]);
whichever works for you.