Jump to content

Recommended Posts

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

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

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

<?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

<?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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.