views:

468

answers:

5

Hello,

I'm building a script which will open a saved text file, export the contents to an array and then dump the contents in a database. So far I've been able to get the file upload working quite happily and can also open said file.

The trouble I'm having is the contents of the file are variable, they have a fixed structure but the contents will change every time. The structure of the file is that each "section" is seperated by a blank line.

I've used php's file() to get an array ... I'm not sure if there's a way to then split that array up every time it comes across a blank line?

  $file = $target_path;
  $data = file($file) or die('Could not read file!');

Example output:

[0] => domain.com
[1] =>  # Files to be checked
[2] =>   /www/06.php
[3] =>   /www/08.php
[4] => 
[5] => domain2.com
[6] =>  # Files to be checked
[7] =>   /cgi-bin/cache.txt
[8] =>   /cgi-bin/log.txt
[9] => 
[10] => domain3.com
[11] =>  # Files to be checked
[12] =>   /www/Content.js
[13] =>

I know that Field 0 and 1 will be constants, they will always be a domain name then that hash line. The lines thereafter could be anywhere between 1 line and 1000 lines.

I've looked at array_chunk() which is close to what I want but it works on a numerical value, what would be good if there was something which would work on a specified value (like a new line, or a comma or something of that sort!).

Lastly, apologies if this has been answered previously. I've searched the usual places a few times for potential solutions.

Hope you can help :) Foxed

+1  A: 

Have you tried split('\n\n', $file); ?

Adrian Sarli
Surely split() works on strings, he's talking about working on an array of strings.
Wesley Mason
That's the same thing I posted :), but @ what Wesley said
Michael
A: 

You could do it by splitting first on the blank line and then on new lines, e.g.:

$file = $target_path;
$fileData = file_get_contents($file) or die('Could not read file!');
$parts = explode("\n\n", $data);
$data = array();
foreach ($parts as $part) {
    $data[] = explode("\n", $part);
}

You could also use preg_split() in place of the first explode() with a regex to sp.lit on lines containing just whitespace (e.g. \s+)

Wesley Mason
+2  A: 

I think what you're looking for is preg_split. If you just split on a carriage return, you might miss lines that just have spaces or tabs.

$output =  array(...);//what you just posted
$string_output = implode('', $output);
$array_with_only_populated_lines = preg_split('`\n\W+`', $string_output);
Robert Elwell
note the actual regex posted above only worked in the dummy example I toyed with before posting. you may need to make a few tweaks to it if it doesn't suite your purposes.
Robert Elwell
Rather than doing file(), implode(), preg_split(), you can use file_get_contents() instead.
Wesley Mason
+1  A: 

You could just do something like this. You could change it also to read the file in line-by-line rather than using file(), which would use less memory, which might be important if you use larger files.

$handle = fopen('blah', 'r');
$blocks = array();
$currentBlock = array();
while (!feof($handle)) {
    $line = fgets($handle);
    if (trim($line) == '') {
        if ($currentBlock) {
            $blocks[] = $currentBlock;
            $currentBlock = array();   
        }
    } else {
        $currentBlock[] = $line;
    }
}
fclose($handle);
//if is anything left
if ($currentBlock) {
    $blocks[] = $currentBlock;   
}

print_r($blocks);
Tom Haigh
That cracked it. First time :) - Thanks to all the other replies, this one solved the problem.
foxed
might solve the trick, but is definitely the least efficient. Wesley had a really good point about just using file_get_contents().
Robert Elwell
Tom Haigh
+1  A: 

I would use the function preg_grep() to reduce the resulting array:

$array = preg_grep('/[^\s]/', $array);
soulmerge