php按条件读取txt广本内容
我现在有个记事本,内容:
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读取这个记事本,因为这个里有好段,每段前都有一个标题,我现在就想通过前面的标题,读取出不同的内容。
请高手给出指教。
------解决方案--------------------
逻辑不对,explode之后的数组元素是不包含@的
修改如下:
<?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); //关闭文件流
?>
<br><font color="#e78608">------解决方案--------------------</font><br>
//标题前加个@符号,用这个来分隔,注意 @ 要顶格<br><br>
$find = 'about'; //要找的标题
$file_name="dir.txt";
$fp=fopen($file_name,'r');
$title = '';
$content = '';
while($buffer = fgets($fp,1024)) //文件不一定要读完
{
if($buffer{0} == '@') { //检查是否为标题
if($title == $find) break; //是要的则退出
$title = trim($buffer, '@'.PHP_EOL); //保存标题
$content = ''; //清空内容
} else $content .= $buffer; //保存内容
}
fclose($fp); //关闭文件流
echo content."<br>";
<br><font color="#e78608">------解决方案--------------------</font><br>如果标题有固定规律的话,读一行,然后检查是不是标题<br><br>比如这一行要没有空格 长度少一多少 等等
<br><font color="#e78608">------解决方案--------------------</font><br>帮忙顶下....<br><div class="clear"></div>
PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号