我與一個PHP文件系統玩弄周圍:strpos()不起作用?
我的PHP文件:
<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>
的data.bdf文件: 是的,我做我自己的文件類型只是爲了好玩... :)
`//This is a comment
`This is not a comment
正如你可能告訴我,我正在閱讀bdf文件,並試圖在屏幕上顯示所有評論(在刪除後以//開頭的行)綠色。這沒有發生,但爲什麼?我認爲這是一個問題在這裏:
$fileline[$count]="<div class=\"comment\">".$fileline[$count]."</div>";
HTML輸出是:
<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>
謝謝你這麼多提前幫助
什麼是編輯? – diracdeltafunk 2011-06-16 17:33:43
編輯顯示在這裏:http://stackoverflow.com/posts/6376080/revisions他刪除了css標籤。 – 2011-06-16 17:38:38
1)http://php.net/manual/en/language.variables.scope.php - 你需要將你的$ fileline信息傳遞給函數interpret()。另外,2)考慮使用foreach循環代替。 – Charx 2011-06-16 17:41:05