I am reading text in from a file and parsing it for display to a browser, all is fine there. Problem occurs when I want to edit this file.
I remove sections of the file by reading the contents into an array, editing the array and writing the array back to the file. Problem is that the text that is now in the file will not parse for display in the browser the way it did before.
Is there some weird stuff happening from to the text from file -> array -> file? The text is still formatted correctly in the text file after the rewrite, can anyone help?
text parsing problem
Moderator: General Moderators
- bradvan
- Forum Newbie
- Posts: 10
- Joined: Mon Jun 02, 2003 8:26 pm
- Location: Rylstone, Australia
- Contact:
In the test file I am using "section" and "endsection" to "contain" the seperate blocks of content I want written to the webpage. The script writes what is in each block to a seperate table cell. After my edit script has rewritten the file the "section" and "endsection" tags get included and the content is not added to a table cell.
here is my edit function:
function delete_content()
{
$page = $_GET["page"];
$heading = $_GET["heading"];
$heading = "<h2>$heading</h2>";
$section = "section";
$endsection = "endsection";
if (file_exists($page) && is_writable($page))
{
$contents = file($page);
$pos = 0;
foreach($contents as $line)
{
$delete_section = false;
if (trim($line) == $section) $start_del = $pos;
if (eregi($heading, $line)) $del_section = true;
if ((trim($line) == $endsection) && ($del_section == true))
{
$end_del = $pos;
$del_size = $end_del - $start_del + 1;
array_splice($contents, $start_del, $del_size);
break;
}
$pos++;
}
$fh = fopen($page, "w");
foreach ($contents as $line)
{
fwrite($fh, $line);
}
fclose($fh);
}
}
here is my parsing function:
function add_content()
{
$fh = fopen("index.txt", "r");
while (!feof($fh))
{
$line = fgets($fh);
$line = trim($line);
if ($line == "section" || $line == "endsection")
{
if ($line == "section") print "<tr><td class=\"leftbox\">";
if ($line == "endsection") print "</td></tr>";
}
else print "$line";
}
fclose($fh);
}
and here is an e.g of the text file:
section
<hr>test</hr>
<p>some text in here</p>
endsection
section
<hr>more</hr>
<p>blah</p>
endsection
here is my edit function:
function delete_content()
{
$page = $_GET["page"];
$heading = $_GET["heading"];
$heading = "<h2>$heading</h2>";
$section = "section";
$endsection = "endsection";
if (file_exists($page) && is_writable($page))
{
$contents = file($page);
$pos = 0;
foreach($contents as $line)
{
$delete_section = false;
if (trim($line) == $section) $start_del = $pos;
if (eregi($heading, $line)) $del_section = true;
if ((trim($line) == $endsection) && ($del_section == true))
{
$end_del = $pos;
$del_size = $end_del - $start_del + 1;
array_splice($contents, $start_del, $del_size);
break;
}
$pos++;
}
$fh = fopen($page, "w");
foreach ($contents as $line)
{
fwrite($fh, $line);
}
fclose($fh);
}
}
here is my parsing function:
function add_content()
{
$fh = fopen("index.txt", "r");
while (!feof($fh))
{
$line = fgets($fh);
$line = trim($line);
if ($line == "section" || $line == "endsection")
{
if ($line == "section") print "<tr><td class=\"leftbox\">";
if ($line == "endsection") print "</td></tr>";
}
else print "$line";
}
fclose($fh);
}
and here is an e.g of the text file:
section
<hr>test</hr>
<p>some text in here</p>
endsection
section
<hr>more</hr>
<p>blah</p>
endsection