url.txt文件中含有:
http://mail.jxxm.com
http://mail.xinglongmuye.com/mail/postoffice/login.php
http://www.yny18.com
上面的三行内容,一行一个
现要再写一行内容到url.txt中,如果url.txt文件中有此内容,就不写,如果没有,就写入,怎么实现呀
多谢
http://mail.jxxm.com
http://mail.xinglongmuye.com/mail/postoffice/login.php
http://www.yny18.com
上面的三行内容,一行一个
现要再写一行内容到url.txt中,如果url.txt文件中有此内容,就不写,如果没有,就写入,怎么实现呀
多谢
解决方案 »
- smarty,输入怎么是空值,求助各位大虾
- 为什么我上传文件只能是图片类型的呢,其他类型的如doc文件都不能上传
- 面向对象 的问题
- zendguard买不起,大家都用什么来给代码加密?
- fopen($path,$flag) 同时工作与windows 和 linux?
- 那位高手给我一些php指点,谢谢
- 弄了个网站,方便大家找工作的,给点意见(留言有分)
- 实在没办法了,大家救急,完不成就不能回家了include问题
- mysql安装后出现问题
- $str=preg_replace("/\[attach\]/is","111",$str); 这一句如何把111替换成可以执行的PHP代码
- php 域名验证
- 数组拆分的问题,在线等
#include <stdlib.h>
#include <stdio.h>
void main()
{
FILE *fp;
char s[]="http://topic.csdn.net/u/20100227/11/e472c5e4-44be-4674-862f-43ebacf45e3f.html";if((fp=fopen("url.txt","a"))==NULL)
{
printf("can not open this file\n");
exit(0);
}fprintf(fp,"%s\n",s);
fclose(fp);}
不太清楚如何判断重复,仅供参考……
如果url.txt文件中有此内容,就不写,如果没有,就写入,怎么实现呀 (用php代码)
$url = "http://www.baidu.com";
$handle = fopen('url.txt','a+');
if($handle){
while (!feof($handle)){
$content = fgets($handle);
if($content == $url){
echo '已存在该内容';
return false;
}
}
if(fwrite($handle,"\r\n".$url ) === false){
echo '写入失败';
return false;
}else{
echo '写入成功';
return true;
}
}
?>
$file_content = file("文件");
$num = count($file_content);
if($num == 0){
//写进文件
} else {
//判断$in_content是否在$file_content 里面
//如果在就不写,否则写进文件}
//关闭文件?>
然后strpos检测待加入的字符串是否存在。不存在就附加到读出来的字符串最后,然后file_put_contents
<?php
$url = "http://www.baidu.com";$content = file_get_contents('C:/Users/alsove/Desktop/url.txt');$preg[0] = "/\./";
$preg[1] = "/\//";
$replace[0] = "\\.";
$replace[1] = "\\/";
$url2 = preg_replace($preg,$replace,$url);preg_match("/\n/",$content,$arr);
$reg = isset($arr[0])?"/".$url2."\n|\n".$url2."/":"/".$url2."/";preg_match($reg,$content,$arr2);
if(!isset($arr2[0])){
$content.=$url."\n";
file_put_contents('C:/Users/alsove/Desktop/url.txt',$content);
}?>
<?php
$url = "http://www.baidu.com";
$content = file_get_contents('url.txt'); /*取得文件内容*/$arr = explode("\n",$content); /*将文件内容打散为数组*/
$arr_leng = count($arr);
$donthave = true;/*一旦在文件内发现与$url相同的内容,就将$donthave设为false并退出循环*/
for($i=0;$i<$arr_leng;$i++){
if($arr[$i]==$url){
$donthave = false;
break;
}
}if($donthave == true){
$content.=$url."\n";
file_put_contents('url.txt',$content);
}
?>
碰到http://www.baidu.com/s?http://www.abc.com这种情况怎么办?
if(!in_array($url,file("url.txt"))){
@file_put_contents('url.txt',@file_get_contents('url.txt').$url."\r\n");
}
}