2012-02-16 125 views
0

我想打开文件,检查文件中是否存在字符串deosn't写入它。Strstr返回false而不是true

这样做:

$fp=fopen('categories.txt','a+'); 
$content=fread($fp,filesize('categories.txt')); 

if(!strstr($content,$cat)){ 
    fwrite($fp,','.$cat); 
} 
fclose($fp); 

但我得到了写后重复在categories.txt中值。 唯一的问题是我可以期待的是编码问题,但所有文件都是utf-8并且在categories.txt我只有拉丁符号和几个符号。

任何想法是哪里的问题?

+1

也许你可以试试[mb_strpos](http://php.net/manual/en/function.mb-strpos.php)呢? – powerbuoy 2012-02-16 09:50:52

回答

0

好吧,我想在fopen问题。我改成这样,代码开始工作:

$content=file_get_contents('categories.dat'); 
$type=(string) $type; 
$content=(string)$content; 

if(!strstr($content,$type)){ 
    file_put_contents('categories.dat',$content.','.$type); 
} 

感谢您的帮助。

2

试试这个。

$pos = strpos($content, $cat); 
if($pos === false){ 
fwrite($fp,','.$cat); 
} 
+0

与strpos和mb_strpos相同的问题。 – Narek 2012-02-16 09:56:17

+0

我尝试了一个正常工作的示例代码。 你可以确保,在你写下你已经使文件清洁的东西之前。 – rajesh 2012-02-16 11:25:53

+0

是的,我在运行代码之前每次都截断文件。 – Narek 2012-02-16 11:51:08

相关问题