dongpiao9078 2011-06-16 17:31
浏览 61
已采纳

strpos()不起作用?

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

  • 写回答

1条回答 默认 最新

  • douhao123457 2011-06-16 17:34
    关注

    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);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法