我正在尝试用PHP在fopen("test.txt","a")中创建一个文本文件。我也尝试过使用fopen("test.txt","w+")。
文本文件已创建,但我想检查test.text中的某些字符串是否存在,或者在test.txt中它不会创建重复条目。有人能帮帮我吗?
发布于 2011-07-01 15:13:28
使用以下代码:
$content = file_get_contents('test.txt');
if (str_pos('YOUR KEYWORD', $content) !== false){
// your keyword exists in the file
}发布于 2011-07-01 15:12:51
if (!file_exists("test.txt"))
{
///file does not exist
}
else
{
$f = fopen("test.txt", "a");
fwrite($f, "appending text");
}在文件中搜索:
$arr = file_get_contents("test.txt");
if (preg_match("/your_rexexp/i", $arr))
{
echo "text found";
}发布于 2011-07-01 15:13:31
如果我理解正确的话,您希望在文本文件中插入新值,但首先检查该值是否已存在于其中。
您可以使用fread()获取文件的内容,然后通过preg_match()检查文件中的值。
https://stackoverflow.com/questions/6544731
复制相似问题