rockinaway Posted September 10, 2007 Share Posted September 10, 2007 What functions would I use if I want to compare the text from one file with another, and then hightlight the missing lines? Link to comment https://forums.phpfreaks.com/topic/68725-compare-files/ Share on other sites More sharing options...
cooldude832 Posted September 10, 2007 Share Posted September 10, 2007 What sort of comparision are you talking? Line by Line, like Text1 a cat a dog a fish Text 2 a snail a cat a turtle Each of those is lines in the files in one case no lines match; matching A-A B-B C-C but in other way A of 1 matches B of 2 so which case is it? Link to comment https://forums.phpfreaks.com/topic/68725-compare-files/#findComment-345474 Share on other sites More sharing options...
rockinaway Posted September 10, 2007 Author Share Posted September 10, 2007 The one where the whole text is search for a match.. so not in order.. Link to comment https://forums.phpfreaks.com/topic/68725-compare-files/#findComment-345477 Share on other sites More sharing options...
Wuhtzu Posted September 10, 2007 Share Posted September 10, 2007 So you mean letter by letter? "A dog named Pluto" and "A dog named pluto" should be different? Link to comment https://forums.phpfreaks.com/topic/68725-compare-files/#findComment-345479 Share on other sites More sharing options...
rockinaway Posted September 10, 2007 Author Share Posted September 10, 2007 Right I have 2 files.. File 1: Hello Test File Test2 File 2: Test Hello File When I compare them I want to check each of File 1s lines to see if they are in the other file.. so here all of File 1s and in File 2 except for Test2, so I want to highlight Test2.... understand? Is this possible? Link to comment https://forums.phpfreaks.com/topic/68725-compare-files/#findComment-345481 Share on other sites More sharing options...
cooldude832 Posted September 10, 2007 Share Posted September 10, 2007 file_get_content(), in_array() Look em up. export each file into a php array (by line i.e file_get_contents) Then say while(isset($file1[$i]){ //Compare to the array $file2} if its match don't say anything else flag it do that for file2 Then just output the files and if that part of the array is flagged highlight it, pretty straight forward Link to comment https://forums.phpfreaks.com/topic/68725-compare-files/#findComment-345489 Share on other sites More sharing options...
rockinaway Posted September 10, 2007 Author Share Posted September 10, 2007 Can you please give an example of it? I kinda got confused :-\ Link to comment https://forums.phpfreaks.com/topic/68725-compare-files/#findComment-345491 Share on other sites More sharing options...
cooldude832 Posted September 10, 2007 Share Posted September 10, 2007 <?php $file1 = "something.txt"; $file2 = "somethingelse.txt"; $file1 = file($file1); $file2 = file($file2); foreach($file1 as $key => $value){ if(!in_array($value,$file2)){ $flag_1[$key] = "yes"; } } foreach($file2 as $key => $value){ if(!in_array($value,$file1)){ $flag_2[$key] = "yes"; } } echo "<pre>".$file1."</pre>"; foreach($file1 as $key => $value){ if($flag_1[$key] == "yes"){ echo "<b>".$value."</b>"; } else{ echo $value; } echo "<br/>"; } echo "<br/><br/><br/>"; echo "<pre>".$file2."</pre>"; foreach($file2 as $key => $value){ if($flag_2[$key] == "yes"){ echo "<b>".$value."</b>"; } else{ echo $value; } echo "<br/>"; } ?> Pretty straightforward I think, I used file here because it will work better than file_get_content Link to comment https://forums.phpfreaks.com/topic/68725-compare-files/#findComment-345502 Share on other sites More sharing options...
rockinaway Posted September 10, 2007 Author Share Posted September 10, 2007 Thanks.. would this work with text that is going to be shown in a textarea? Link to comment https://forums.phpfreaks.com/topic/68725-compare-files/#findComment-345503 Share on other sites More sharing options...
cooldude832 Posted September 10, 2007 Share Posted September 10, 2007 what do you mean shown in a text area? Link to comment https://forums.phpfreaks.com/topic/68725-compare-files/#findComment-345505 Share on other sites More sharing options...
rockinaway Posted September 10, 2007 Author Share Posted September 10, 2007 Notice: Undefined offset: 3 I am getting lots of those errors.. and the results I want to be shown in a textarea... Link to comment https://forums.phpfreaks.com/topic/68725-compare-files/#findComment-345506 Share on other sites More sharing options...
cooldude832 Posted September 10, 2007 Share Posted September 10, 2007 Well tags don't show in a textbox so that won't work, if you can't follow the script, maybe you should read some tutorials on file handlers before attempting this. Link to comment https://forums.phpfreaks.com/topic/68725-compare-files/#findComment-345508 Share on other sites More sharing options...
cooldude832 Posted September 10, 2007 Share Posted September 10, 2007 <?php $file1 = "something.txt"; $file2 = "somethingelse.txt"; $file1 = file($file1); $file2 = file($file2); $flag_1 = array(); $flag_2 = array(); $i = 0; foreach($file1 as $value){ if(!in_array($value,$file2)){ $flag_1[$i] = "yes"; } $i++; } $i = 0; foreach($file2 as $value){ if(!in_array($value,$file1)){ $flag_2[$i] = "yes"; } $i++; } $i = 0; echo "<pre>".$file1."</pre>"; foreach($file1 as $value){ if($flag_1[$i] == "yes"){ echo "<b>".$value."</b>"; } else{ echo $value; } $i++; echo "<br/>"; } echo "<br/><br/><br/>"; echo "<pre>".$file2."</pre>"; $i = 0; foreach($file2 as $value){ if($flag_2[$i] == "yes"){ echo "<b>".$value."</b>"; } else{ echo $value; } $i++; echo "<br/>"; } ?> Its because it won't index thus undefined indexing Link to comment https://forums.phpfreaks.com/topic/68725-compare-files/#findComment-345511 Share on other sites More sharing options...
effigy Posted September 10, 2007 Share Posted September 10, 2007 There's also Unix's diff command. Link to comment https://forums.phpfreaks.com/topic/68725-compare-files/#findComment-345532 Share on other sites More sharing options...
wildteen88 Posted September 10, 2007 Share Posted September 10, 2007 cooldude wow! Your script is over complicated. Your code can be simplified down to just: <?php $file1_words = file('file1.txt'); $file2_words = file('file2.txt'); echo '<h2>File1 Words</h2>'; foreach($file1_words as $word) { if(!in_array($word, $file2_words)) { echo '<b>' . $word . "</b> (<i>match</i>)<br />\n"; } else { echo $word . "<br />\n"; } } echo '<h2>File2 Words</h2>'; foreach($file2_words as $word) { if(!in_array($word, $file1_words)) { echo '<b>' . $word . "</b> (<i>match</i>)<br />\n"; } else { echo $word . "<br />\n"; } } ?> Link to comment https://forums.phpfreaks.com/topic/68725-compare-files/#findComment-345534 Share on other sites More sharing options...
cooldude832 Posted September 10, 2007 Share Posted September 10, 2007 yeah i realized that afterwards, but it does the same thing. That unix idea sounds like an interesting one but requires exec()/a unix server Link to comment https://forums.phpfreaks.com/topic/68725-compare-files/#findComment-345601 Share on other sites More sharing options...
cooldude832 Posted September 11, 2007 Share Posted September 11, 2007 any idea if it works for you?? Link to comment https://forums.phpfreaks.com/topic/68725-compare-files/#findComment-345702 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.