我在写这PHP通过替换标记的方法从txt中提取数据生成HTML代码的程序时遇到了这样一个问题:第一行读出的数据无效,但值是正确的~list.txt文本格式如下:(字符+\t+字符)
[code]
2    PHP
3    JSP
1    ASP[/code]PHP代码如下:<?
$lists = file("page/list.txt");
$friends = file("page/friends.txt");
$listLenght = count($lists);
$template = 'template\1.html'; //模板地址
//shuffle($linedata); //用于打乱数组的顺序$HTML_list = ''; //列表
foreach ($lists as $list) {
$detalist = @explode("\t", chop($list));
$HTML_list .= " \t\t\t<li><a href=\"list_{$detalist[0]}.html\" alt=\"{$detalist[1]}\">{$detalist[1]}</a></li>\n";
}
$alldate = file_get_contents($template);
$alldate = ereg_replace("##HTML_list##", $HTML_list, $alldate);//企业联盟
$HTML_friends = '';
foreach ($friends as $key => $friend) {
$detafri = @explode("\t", chop($friend));
if(isset($detafri[1])) $HTML_friends .= "<a href=\"http://{$detafri[0]}\" alt=\"{$detafri[1]}\">{$detafri[1]}</a>";
}
$alldate = ereg_replace("##HTML_friends##", $HTML_friends, $alldate);
foreach ($lists as $list) {
$HTML_word = ''; //关键词
$HTML_title = ''; //标题
$temp = $alldate;
$detalist = @explode("\t", chop($list));
$file = "page/{$detalist[0]}.txt";
$linedata = @file($file);
if (!$linedata) {
echo "{$file}文件不存在!<br /><br />\n";
continue;
}
// shuffle($linedata);
$count = count($linedata);
for($i = 0; $i < $count; $i++) {
$detail = @explode("\t", chop($linedata[$i]));
// 记录显示
if(isset($detail[1])) $HTML_word .= "\t\t<li><a href=\"http://{$detail[0]}\" target=\"_blank\">{$detail[1]}</a></li>\n"; 
}
$temp = ereg_replace("##HTML_word##", $HTML_word, $temp); $HTML_title = chop($detalist[1]);
$temp = ereg_replace("##HTML_title##", $HTML_title, $temp);
$filepost = "html/list_{$detalist[0]}.html";
file_put_contents($filepost, $temp);
echo "完成<a href=\"$filepost\">$filepost</a><br />";
}
?>
page/0.txt,page/1.txt之类的是内容数据,格式如下(一个网址+\t+网站名):
[code]
www.cctv.com    中央电视台
www.ourgame.com    联众游戏[/code]template/aa.html是模板,主要代码如下:
[code]<ul>
##HTML_list##
</ul>
…………
<ul>
##HTML_word##
</ul>[/code]

解决方案 »

  1.   

    list.txt文本格式如下:(字符+\t+字符) 
    2    PHP
    3    JSP
    1    ASPtemplate/aa.html是模板,主要代码如下:
    <ul>
    ##HTML_list##
    </ul>
    …………
    <ul>
    ##HTML_word##
    </ul>
      

  2.   

    $file = "page/{$detalist[0]}.txt";
    $linedata = @file($file);我在这里加入echo urlencode($file)."\n";发现输出的结果中包含了看不到的字符,这就难怪了,但怎么样去掉呢?[code] $file = "page/{$detalist[0]}.txt";
    echo urlencode($file)."\n";
    $linedata = @file($file);
    [/code]输出的结果是这样的
    page%2F%EF%BB%BF1.txtpage/1.txt文件不存在!<br /><br />
    page%2F2.txt一看就知道page%2F%EF%BB%BF1.txt中的%EF%BB%BF1是看不到的非打印字符,但怎么样去掉呢?