我现在有个记事本,内容:
articles
The research articles in all journals published by BioPublisher are 'Open Access'. They are immediately and permanently available online without charge. A number of journals require an institutional or a personal subscription to view other content, such as reviews or paper reports. Free trial subscriptions to these journals are available.
about
tel:********************** address:*************************************
我现在就用PHP读取这个记事本,因为这个里有好段,每段前都有一个标题,我现在就想通过前面的标题,读取出不同的内容。
请高手给出指教。
articles
The research articles in all journals published by BioPublisher are 'Open Access'. They are immediately and permanently available online without charge. A number of journals require an institutional or a personal subscription to view other content, such as reviews or paper reports. Free trial subscriptions to these journals are available.
about
tel:********************** address:*************************************
我现在就用PHP读取这个记事本,因为这个里有好段,每段前都有一个标题,我现在就想通过前面的标题,读取出不同的内容。
请高手给出指教。
$file_name="dir.txt";
$fp=fopen($file_name,'r');
while(!feof($fp)) //文件全部要读完
{
$buffer=fgets($fp,1024); //获得一行
$period = explode("@",$buffer); //以@分隔分行内容
if($period[1]!='@'){ //找到第2部分的内容,如果不是@
echo $buffer."<br>";
}
}
fclose($fp); //关闭文件流
?>
但是还是全部输出 。
修改如下:
<?php
$file_name="dir.txt";
$fp=fopen($file_name,'r');
while(!feof($fp)) //文件全部要读完
{
$buffer=fgets($fp); //获得一行,不要加长度
if(preg_match('/\s*@/i', $buffer)){// 用正则
echo "This is title<br>";
echo $buffer."<br>";
}else{
echo "This is body<br>";
echo $buffer."<br>";
}
}
fclose($fp); //关闭文件流
?>
如果按条件输出如何写?比如我只想输出articles或about下面内容,我不想全部输出。