文本处理 - PHP读写txt文件的问题
伊谢尔伦
伊谢尔伦 2017-04-10 16:51:25
[PHP讨论组]

代码如下:
<!doctype html>
<html>
<head>
<title>四位.club 域名</title>
<meta charset="utf-8">
</head>
<body>
<?php
/*
test.txt 文件内容为:

10ferents.club
1901.club
1992.club
288cash.club
2nd.club
365gold.club
3dscanning.club
406disco.club
99fitness.club
abuo9i8n3.club
abuo9i8n5.club
abur5t6b2.club
abut6y7n1.club
abut6y7n4.club
abut6y7n7.club
abuy7u8n6.club
1234.club
abcd.club

4.txt 只有:
abcd.club

正确的答案应该是:
1901.club
1992.club
1234.club
abcd.club
*/
$handle=fopen("test.txt","r");
while(!feof($handle)){
$str=fgets($handle);
if(stripos($str,".")==4){
file_put_contents("4.txt",$str);
}
}
?>
</select>

</body>
</html>
筛选域名用的,但是结果怎么只显示一行啊,用
echo $str."
\n";显示结果在正常的.

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

全部回复(4)
天蓬老师

这样:

$str = fgets($handle); 
$str = trim($str);

不知道的怎么解决的时候,请

var_dump(stripos($str,"."));

看看是否是4

还有追加写入:

file_put_contents("4.txt",$str, FILE_APPEND);
伊谢尔伦

写了个DEMO,用正则匹配的

$str = '10ferents.club 
1901.club 
1992.club 
288cash.club 
2nd.club 
365gold.club 
3dscanning.club 
406disco.club 
99fitness.club 
abuo9i8n3.club 
abuo9i8n5.club 
abur5t6b2.club 
abut6y7n1.club 
abut6y7n4.club 
abut6y7n7.club 
abuy7u8n6.club 
1234.club 
abcd.club';
preg_match_all('/[0-9a-z-]{4}/', $str, $arr);
$string = implode("\n", $arr[0]);
file_put_contents('1.txt', $string);

截图:


file_put_contents每次都会覆盖掉之前的,应该用fwrite追加写入

PHP中文网

file_put_contents("4.txt",$str,FILE_APPEND);

天蓬老师

fopen("test.txt","a")

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号