【问题标题】:Strstr return false instead of truestrstr 返回 false 而不是 true
【发布时间】:2012-02-16 09:45:17
【问题描述】:

我要打开文件,检查文件中是否不存在字符串deos写入。

这样做:

$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 我只有拉丁符号和几个符号。

有什么想法是哪里出了问题?

【问题讨论】:

标签: php fopen fwrite fread strstr


【解决方案1】:

像这样试试。

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

【讨论】:

  • strposmb_strpos 有同样的问题。
  • 我尝试了一个可以正常工作的示例代码。你能确保在你写东西之前你已经把文件清理干净了。
【解决方案2】:

好的,我认为fopen 有问题。我改成这个,代码开始工作了:

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

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

感谢您的帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-03-23
    • 2014-04-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多