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!
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
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.
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/
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);
?>
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);