suitedupgeek
suitedupgeek

Reputation: 885

php function to split an array at each blank line?

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

Upvotes: 2

Views: 3870

Answers (5)

Tom Haigh
Tom Haigh

Reputation: 57825

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

Upvotes: 1

soulmerge
soulmerge

Reputation: 75724

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

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

Upvotes: 0

Robert Elwell
Robert Elwell

Reputation: 6668

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

Upvotes: 3

Wes Mason
Wes Mason

Reputation: 1628

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

Upvotes: 0

Adrian Sarli
Adrian Sarli

Reputation: 2356

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

Upvotes: 1

Related Questions