Jump to content

String comparison


nikini

Recommended Posts

If anybody knows Google Docs. They have a revision history. And when you compare two docs they say what has been added and what has been removed. I've done something similar. But it's now working very good and i want to change the hall thing.

So...can anybody tell me.. is there a library of some sort? I got something called inline_diff. But it's not very good. It does just word comparison and I don't need just word comparison, but also phrase comparison and paragraph comparison if case. That means that let's say if 50% of a phrase has changed then the hall phrase has changed. The same for the paragraph. Anybody know how to do that?

Link to comment
Share on other sites

:o:O complexity O(N**3) that is huge. and it does it at character level. But i figured out a solution. The problem is that i want a function that fixes a html text. CAn anybody tell me how to do that? Some function that closes open tags and so on..
Link to comment
Share on other sites

:o:O complexity O(N**3) that is huge. and it does it at character level. But i figured out a solution. The problem is that i want a function that fixes a html text. CAn anybody tell me how to do that? Some function that closes open tags and so on..

 

The cleaning of HTML tags is complex in itself. Let's say I have this

 

<html>
<head>
<body>
<p>
<div>
    <a>
    <img>

How would you know where the closing </p> goes or the closing </div> goes. Does the <a> need to be closed before or after the image?

 

That is the huge headache, for the most part you know the head should be closed before body, but what if the body tag is omitted? =)

 

 

Link to comment
Share on other sites

aah... found it rephrased on php.net... basically... it returns a %age of the similarity between the two strings :-)

 

<?php
function compare_strings($str1, $str2){
$count=0;
$str1=ereg_replace("[^a-z]", ' ', strtolower($str1));
while(strstr($str1, '  ')) $str1 = str_replace('  ', ' ', $str1);
$str1=explode(' ', $str1);
$str2=ereg_replace("[^a-z]", ' ', strtolower($str2));
while(strstr($str2, '  ')) $str2 = str_replace('  ', ' ', $str2);
$str2=explode(' ', $str2);
if(count($str1)<count($str2)){
  $tmp=$str1;
  $str1=$str2;
  $str2=$tmp;
  unset($tmp);
}
for($i=0; $i<count($str1); $i++) if(in_array($str1[$i], $str2)) $count++;
return $count/count($str2)*100;
}
?>

Link to comment
Share on other sites

1. The function does not work well.. I tested it like this

echo compare_strings("Textul nr 1 este aici","Textul nr 2 este tot aici");

and it outputs 100.

2. It's not what i wanted..but thanks anyway.. I want users to be able to view the differences marked in green if added text or red if deleted

Link to comment
Share on other sites

oooooooo... try this lol

 

function find_differences($string1,$string2){
for($i=0; $i<=strlen($string2); $i++){
  if($string1{$i}==$string2{$i}) $string3.='<span style="color:green;">'.$string2{$i}.'</span>';
  else $string3.='<span style="color:red;">'.$string2{$i}.'</span>';
}
return $string3;
}

 

it'll highlight the differences to the letter of what was changed...

Link to comment
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.