Php - 如何从文本文件中读取类似的字符串?

时间:2015-08-21 10:49:18

标签: php

如何从文本文件中读取字符串并检查IF语句是否存在或类似文本? (如果字符串为extist,如果新信息与文件内部相同,则不更新文本文件,如果是新信息 - 更新文件)

示例:

 if(string exist) {
  // then dont do nothing because string exists
 }
 else {
  // do something like, insert info to text file
 }

THX

2 个答案:

答案 0 :(得分:0)

$file_data = file_get_contents('myfile.txt');
$mystring = 'mystring';
if(strpos($file_data, $mystring) !== false) {
    //the string is found
} else {
    //no such string in the file
}

答案 1 :(得分:-1)

使用以下代码(未测试)将文件内容与字符串进行比较。 您可以通过更改minimumScore来调整相似度。

$textFromFile = file_get_contents($filename); 
$comparison = 'string to compare'; 
$minimumScore = 10;

similar_text($textFromFile, $comparison, $score); 

if ($score > $minimumScore) {
  // then dont do nothing because string exists
} else {
  // do something like, insert info to text file
}