2011-06-16 35 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