Delete Certain String in Text Files Using PHP
<?php
//reference text
foreach (glob("file1.txt") as $filename1) {
$file1 = $filename1;
$line1 = rtrim($file1, "\n");
$contents1 = file($line1, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
$string1 = implode($contents1); // file_put_contents('google.txt',implode('',$contents1));
$array1 = str_split ($string1 );
$temp = $array1;
}
//fetch from website
$url ="https://www.example.com/";
$str = file_get_contents($url);
$myFile = "file2.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $str;
fwrite($fh,$stringData);
fclose($fh);
foreach (glob("fetch_hLeong.txt") as $filename) {
$file = $filename;
$line = rtrim($file, "\n");
$contents = file($line,FILE_IGNORE_NEW_LINES | F开发者_如何学GoILE_SKIP_EMPTY_LINES);
$string = implode($contents);
$array = str_split($string);
$temp1 = $array;
}
//compare $result = array_diff_assoc($temp, $temp1); if (!count($result)) echo "
File => MATCH"; else echo "File => NOT MATCH";?>
So i have this type of code, im going to compare those file1.txt and file2.txt. Here i put each of the character in text files into an array and i compare it character by character. So my problem is, while comparing the code show NOT MATCH, which actually it is match. The www.example.com has news feed that make it change every second because of that my files show NOT MATCH.
i want to delete certain string which is the newsfeed in the file1.txt so that it wont effect the process. Anyone ?? Thanks for your help .
- Instead of using file+implode, use file_get_contents
- You cannot use array_diff here since it compares the contents of the array, not the particular order as you need it
- Have a look at PEAR's Text_Diff for creating textual diffs.
精彩评论