开发者

strpos() doesn't work?

i'm toying around with a php filesystem:

my php file:

<html>
<head>
<title>Text Database Editor</title>
<style type="text/css">
div.comment {
    color:green;
}
</style>
</head>
<body>
<?php
function interpret() {
    for ($count=1;!empty($fileline[$count]);$count++) {
        if (strpos($fileline[$count],"//")===0) {
            $fileline[$count]="<div class=\"comment\">".$fileline[$count]."</div>";
        }
    }
}
$filepath = "data.bdf";
//in bdf files, each line starts with ` and commented lines begin with开发者_如何学编程 `//
$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);
interpret();
for ($count=1;!empty($fileline[$count]);$count++) {
    echo $count.": ".$fileline[$count]."<br />";
}
?>
</body>
</html>

the data.bdf file: yes, i made my own filetype just for fun... :)

`//This is a comment
`This is not a comment

as you can probably tell, i'm reading the bdf file and trying to make all comments (lines that begin with // after the ` is removed) green when they are displayed on screen. this is not happening, but why? i think it's a problem here:

$fileline[$count]="<div class=\"comment\">".$fileline[$count]."</div>";

the html output is:

<html>
<head>
<title>Text Database Editor</title>
<style type="text/css">
div.comment {
    color:green;
}
</style>
</head>
<body>
File being opened contains 44 bytes of data. Opening file...<br />File Opened!<br />File Read!<br /><br />Data in file:<br /><br />1: //This is a comment
<br />2: This is not a comment<br /></body>
</html>

thank you so much for helping in advance


Your function interpret() references $fileline, which is a global variable, but does not use the global keyword.

Instead, pass $fileline to interpret() as an argument:

// Argument reference &$fileline
function interpret(&$fileline) {
  for ($count=1;!empty($fileline[$count]);$count++) {
    if (strpos($fileline[$count],"//")===0) {
        $fileline[$count]="<div class=\"comment\">".$fileline[$count]."</div>";
    }
  }
}

// Later, your function call...
$fileline = explode("`",$filedata);
interpret($fileline);

Note that above interpret() receives its argument by reference. I'm not crazy about this, and you could also return $fileline at the end of the function and assign it with the call:

function interpret($fileline) {
  for ($count=1;!empty($fileline[$count]);$count++) {
    if (strpos($fileline[$count],"//")===0) {
        $fileline[$count]="<div class=\"comment\">".$fileline[$count]."</div>";
    }
  }
  // Return the argument instead
  return $fileline;
}

// Later, your function call...
$fileline = explode("`",$filedata);
$fileline = interpret($fileline);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜