Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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

share|improve this question

5 Answers

up vote 1 down vote accepted

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);
share|improve this answer
That cracked it. First time :) - Thanks to all the other replies, this one solved the problem. – foxed Jul 27 '09 at 15:15
might solve the trick, but is definitely the least efficient. Wesley had a really good point about just using file_get_contents(). – Robert Elwell Jul 27 '09 at 18:33
You're just thinking about CPU. If the file is say 1Gb, then file_get_contents() & converting that to an array will have a much higher peak memory use. I think this is also worth considering. – Tom Haigh Jul 27 '09 at 21:10

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

$array = preg_grep('/[^\s]/', $array);
share|improve this answer

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);
share|improve this answer
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 Jul 27 '09 at 15:01
Rather than doing file(), implode(), preg_split(), you can use file_get_contents() instead. – Wes Mason Jul 27 '09 at 15:02

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+)

share|improve this answer

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

share|improve this answer
Surely split() works on strings, he's talking about working on an array of strings. – Wes Mason Jul 27 '09 at 14:55
That's the same thing I posted :), but @ what Wesley said – Dirk Jul 27 '09 at 14:56

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.