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
https://forums.phpfreaks.com/topic/47711-string-comparison/
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
https://forums.phpfreaks.com/topic/47711-string-comparison/#findComment-233603
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
https://forums.phpfreaks.com/topic/47711-string-comparison/#findComment-233971
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
https://forums.phpfreaks.com/topic/47711-string-comparison/#findComment-235860
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
https://forums.phpfreaks.com/topic/47711-string-comparison/#findComment-236814
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.