Using foreach to modify arrays starting with whitespace in php

Go To StackoverFlow.com

1

First time posting here, fresh into development.

Trying to modify the output of a text file with multiple lines, some start with characters, others with whitespace/tab. I want to have the lines that start with the tab/whitespace to be joined with any line that came directly before it. I've been looking into doing this with regex, but I'm not sure how to structure it. I'm thinking that I need to use 'foreach' to look at each line, find each one with whitespace, and use a series of regex to join them to the preceding line. This is how far I've gotten myself, along with where I believe the foreach should be at.

<?php

$file_handle = fopen("parse.txt", "r");

while (!feof($file_handle)){

    $each_line = fgets($file_handle);
    foreach ($each_line    ){

        }

    print $each_line . "<br />";
}

fclose($file_handle);

?>

The specific text file is at http://krondorkrew.com/parse/parse.txt If you go there and look at the index of the /parse folder, I have my current build of this php, also.

My next step is to separate each line into separate arrays by explode at the first : in each line, but I still want to try fighting that problem myself before looking to you awesome people for help (but if you have any insight, I wouldn't look away)

Thank you in advance for all help!

2012-04-03 21:45
by yarhouse
First, read about foreach loops. Then, continue reading the manual - Josh 2012-04-03 21:47


1

This code does the job:

<?php

$output = "";
foreach (file('parse.txt') as $line) {
  $output .= trim($line);

  // if the current line starts noes not starts with a tab, we append
  // a \n to it
  if (substr($line, 0, 1) != "\t") {
    $output .= "\n";
  }
}
echo $output;

But you should be using regexp for this kind of text manipulation. Codepad example here

2012-04-03 21:57
by Benjamin Crouzier


1

The following is a code-example that is just missing the startsWithWhitespaceOrTab function (which should be easy to write, I leave this as an example). The rest already works:

foreach (file('parse.txt', FILE_IGNORE_NEW_LINES) as $line) {
    if (!startsWithWhitespaceOrTab($line)) {
        echo "\n";
    }
    echo $line;
}

See the PHP file function and the foreach loop.

2012-04-03 21:58
by hakre


0

Use file_get_contents() to read the file into a single string. You can then use regex to match white space, or a new line and then white space, etc.

I would post the regex, but I'm not sure exactly what you're trying to do. You cant test regex here: http://gskinner.com/RegExr/

2012-04-03 21:52
by mowwwalker
filegetcontents, sounds like I might want to go in that direction. I'll have to mess with this more tomorrow implimenting that as a direction for this. And thank you for the link to test out regex, I'll have to bookmark, never knew that tool existed - yarhouse 2012-04-03 22:04


0

You'll probably want something along these lines:

<?php

$file_handle = fopen("parse.txt", "r");

$key = null;
$arr = array();

while (!feof($file_handle)){

    $each_line = fgets($file_handle);

    if ($key && preg_match('/^\s+/', $each_line)) {
        $arr[$key] .= ' ' . trim($each_line);
    } else if (preg_match('/^([a-zA-Z]+):(.*)$/', $each_line, $m)) {
        $key = $m[1];
        $arr[$key] = trim($m[2]);
    }

}

fclose($file_handle);

?>
2012-04-03 21:54
by Pete
I tried copy/paste that right in, didn't output any info, which I noticed there wasn't one. Tried putting a print inside the last bracket using a few of the different variables you created, but wasn't pulling out what I expected. I'll take a closer look tomorrow. Thank you nonetheless - yarhouse 2012-04-03 22:06
Try var_dump($arr) at the end - it's putting the lines into an array - Pete 2012-04-03 22:09


0

Yet another solution. Collects the lines in an array for you. I think I like @pinouchon 's better though.

$file_handle = fopen("parse.txt", "r");
$fixed = array();
while (!feof($file_handle)){
    $line = fgets($file_handle);
    if (ctype_space($line[0])) { // Checks for whitespace
        $fixed[count($fixed)-1].= trim($line); // Adds to previous line
    } else {
        $fixed[] = trim($line); // Otherwise, keep line as is
    }
}
fclose($file_handle);

// Print
echo implode("\n",$fixed); 
2012-04-03 21:59
by savinger