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 phython如何实现以下功能?查找同一用户名的消费金额合并—
  • ¥15 孟德尔随机化怎样画共定位分析图
  • ¥18 模拟电路问题解答有偿速度
  • ¥15 CST仿真别人的模型结果仿真结果S参数完全不对
  • ¥15 误删注册表文件致win10无法开启
  • ¥15 请问在阿里云服务器中怎么利用数据库制作网站
  • ¥60 ESP32怎么烧录自启动程序
  • ¥50 html2canvas超出滚动条不显示
  • ¥15 java业务性能问题求解(sql,业务设计相关)
  • ¥15 52810 尾椎c三个a 写蓝牙地址