这是页面:[我删除了网址,它不再存在]
我的问题是data.bdf中的注释除了第一个之外没有格式化。
这是php:
<html>
<head>
<title>Text Database Editor</title>
<style type="text/css">
span.comment {
color:green;
}
</style>
<script type="text/javascript">
function setURLValue() {
document.getElementById("url").value=window.location;
}
</script>
</head>
<body onLoad="setURLValue();">
<?php
function interpret($fileline) {
for ($count=1;!empty($fileline[$count]);$count++) {
if (strpos($fileline[$count],"//")==0) {
$fileline[$count]=substr($fileline[$count],2);
$fileline[$count]="<span class=\"comment\">".$fileline[$count]."</span>";
}
return $fileline;
}
}
$filepath = "data.bdf";
$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);
$fileline = interpret($fileline);
for ($count=1;!empty($fileline[$count]);$count++) {
echo $count.": ".$fileline[$count]."<br />";
}
?>
<br />Write to file:
<form name="writeto" action="write_to_file.php" method="post">
<input type="hidden" name="formurl" id="url" />
<input type="hidden" name="file" value="<?php echo $filepath;?>" />
<input type="radio" name="iscomment" value="Yes" />Comment
<input type="radio" name="iscomment" value="No" />Data<br />
Text: <input type="text" name="text" />
<input type="submit" />
</form>
</body>
</html>
<?php fclose($handle);?>
感谢您的帮助...
顺便说一句,我不再有写两次的错误
答案 0 :(得分:3)
你应该使用!== false
,即
if (strpos($fileline[$count], "//" ) !== false) {
答案 1 :(得分:3)
return
中的interpret()
声明位置错误。它应该在循环之外:
function interpret($fileline) {
for ($count=1;!empty($fileline[$count]);$count++) {
if (strpos($fileline[$count],"//")===0) {
$fileline[$count]=substr($fileline[$count],2);
$fileline[$count]="<span class=\"comment\">".$fileline[$count]."</span>";
}
}
return $fileline;
}