Jump to content

Text Difference


verdrm

Recommended Posts

Can you make this clearer? Are you asking to check two see if two pieces of text are the same or different?

 

<?php
$var1 = "hello";
$var2 = "goodbye";
$var3 = "hello";

if($var1 == $var2){//this isn't true
echo "The first variable does equal the second variable.";
} else {
echo "The first variable does not equal the second variable.";
}

if($var1 == $var3){//this is true
echo "The first variable does equal the third variable.";
} else {
echo "The first variable does not equal the third variable.";
}
?>

In the case above.. You get this output:

 

The first variable does not equal the second variable.
The first variable does equal the third variable.

Link to comment
https://forums.phpfreaks.com/topic/73212-text-difference/#findComment-369372
Share on other sites

Does anyone know how to tell the difference between two sets of text. This needs to be compatible with PHP4. I wrote a little something but it is missing the way to tell the difference. I would really like this to either COLOR, HIGHLIGHT, or BOLD the different text.

 

 

<?php

$ted='echo 123';

$fred='echo 1234';

echo $ted;

echo $fred;

if($ted !== $fred){

echo'';}

?>

 

Link to comment
https://forums.phpfreaks.com/topic/73212-text-difference/#findComment-369705
Share on other sites

try this:

<?php
$ted = 'echo 123';
$fred = 'echo 1234';
echo "" . $ted . "<br>";
echo "" . $fred . "<br>";
if($ted !== $fred){
echo "<b> ted does not equal fred</b>"; 
}
?> 

 

or echo $fred, and you can use HTML color tags between the double comma's.

 

I'm not that good at php, just started.

 

if($ted !== $fred)

 

instead of using !== , why don't u simply use !=

 

???

 

you are better off using 2 equal signs to get a more absolute answer.

 

Regards ACE

Link to comment
https://forums.phpfreaks.com/topic/73212-text-difference/#findComment-369734
Share on other sites

This function will show (bold) the difference between the $target string and the $compare string. Note it will only bold the different characters it finds in $compare. If you want to highlight the difference both ways it will need a fair bit more work, but this will get you a start.

 

<?php

   $ted='this is text';
   $fred='this is more text';

   function highlight_diff($target,$comparison) {
       $tmp = '';
       for ($i = 0; $i <= strlen($target)-1; $i++) {
           if (strlen($comparison) <= $i) {
               return $tmp . substr($target,strlen($comparison));

           }  

           if ($target{$i} != " ") {
               if ($target{$i} == $comparison{$i}) {
                   $tmp .= $comparison{$i};
   
               } else {
                   $tmp .= "<b>" . $comparison{$i} . "</b>";
   
               }  

           } else {
               $tmp .= " ";

           }  
   
       }  

       if (strlen($comparison) > strlen($target)) {
           $tmp .= "<b>" . substr($comparison,strlen($target)) . "</b>";

       }  

       return $tmp;
   
   }  

   echo highlight_diff($ted,$fred);

?>

 

Be warned, I only just quickly wrote this so its probably riddled with bugs.

Link to comment
https://forums.phpfreaks.com/topic/73212-text-difference/#findComment-369747
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.