[英]strpos() doesn't work?
我在玩弄php文件系统:
我的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>
非常感谢您的提前帮助
您的函数interpret()
引用$fileline
,这是一个全局变量,但不使用global
关键字。
而是将$fileline
作为参数传递给interpret()
:
// 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);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.