Question

here is the page: [I removed the URL, it doesn't exist anymore]

my problem is that comments in the data.bdf are not formatted except for the first one.

this is the php:

<html>
<head>
<title>Text Database Editor</title>
<style type="text/css">
span.comment {
    color:green;
}
</style>
<script type="text/javascript">
function setURLValue() {
    document.getElementById("url").value=window.location;
}
</script>
</head>
<body onLoad="setURLValue();">
<?php
function interpret($fileline) {
    for ($count=1;!empty($fileline[$count]);$count++) {
        if (strpos($fileline[$count],"//")==0) {
            $fileline[$count]=substr($fileline[$count],2);
            $fileline[$count]="<span class=\"comment\">".$fileline[$count]."</span>";
        }
        return $fileline;
    }
}
$filepath = "data.bdf";
$filesize = @filesize($filepath);
if (!empty($filesize)) {
    echo "File being opened contains ".$filesize." bytes of data. Opening file...<br />";
}
else {
    echo "Error in determining file size. ";
}
$handle = @fopen($filepath, "r") or die("File could not be opened");
echo "File Opened!<br />";
$filedata = fread($handle, $filesize+1) or die("File could not be read");
echo "File Read!<br /><br />Data in file:<br /><br />";
$fileline = explode("`",$filedata);
$fileline = interpret($fileline);
for ($count=1;!empty($fileline[$count]);$count++) {
    echo $count.": ".$fileline[$count]."<br />";
}
?>
<br />Write to file:
<form name="writeto" action="write_to_file.php" method="post">
<input type="hidden" name="formurl" id="url" />
<input type="hidden" name="file" value="<?php echo $filepath;?>" />
<input type="radio" name="iscomment" value="Yes" />Comment
<input type="radio" name="iscomment" value="No" />Data<br />
Text: <input type="text" name="text" />
<input type="submit" />
</form>
</body>
</html>
<?php fclose($handle);?>

Thanks for helping...

by the way i no longer have the bug with writing twice

Was it helpful?

Solution

The return statement in interpret() is at the wrong place. It should be outside the loop:

function interpret($fileline) {
    for ($count=1;!empty($fileline[$count]);$count++) {
        if (strpos($fileline[$count],"//")===0) {
            $fileline[$count]=substr($fileline[$count],2);
            $fileline[$count]="<span class=\"comment\">".$fileline[$count]."</span>";
        }
    }
    return $fileline;
}

OTHER TIPS

You should use !== false, i.e.

if (strpos($fileline[$count], "//" ) !== false) {
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top