2011-06-16 82 views
0

我與一個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> 

謝謝你這麼多提前幫助

+0

什麼是編輯? – diracdeltafunk 2011-06-16 17:33:43

+0

編輯顯示在這裏:http://stackoverflow.com/posts/6376080/revisions他刪除了css標籤。 – 2011-06-16 17:38:38

+0

1)http://php.net/manual/en/language.variables.scope.php - 你需要將你的$ fileline信息傳遞給函數interpret()。另外,2)考慮使用foreach循環代替。 – Charx 2011-06-16 17:41:05

回答

5

你的功能interpret()引用$fileline,這是一個全局變量,但不使用global關鍵字。

相反,通過$filelineinterpret()作爲參數:

// 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); 

注意,上述interpret()通過引用接收其參數。我不是瘋了這一點,而你也return $fileline可以在功能的結束和與呼叫分配給它:

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

不起作用... – diracdeltafunk 2011-06-16 17:38:15

+0

@ Ben7005是的,因爲我把它放在我以後的編輯中。重新定義你的函數,需要'$ fileline'參數並按照你的猜測調用它。 – 2011-06-16 17:38:26

+0

@Michael其實,你需要傳遞變量作爲參考:'function interpret(&$ fileline){';否則,在調用函數時,函數外的實際'$ fileline'不會改變。 – mc10 2011-06-16 17:39:19