I want to create a method which compares two sentence and returns the words that makes them different.
e.g
grey tiles 100 X 200 kitchen flooring
grey tiles 300 X 600 kitchen flooring
difference are 100 x 200 300 x 600
it will allow one single word (not necesary one character to sit between two unique words) though if the distance exceeds by one it will throw an error saying the phrases are not similar enough the difference can be found at the start/ end / middle of the text
after passing to different blogs, i found similar approach to my concern using the native php function xdiff_string_diff but the problem is, I cant make the code works. I got error saying xdiff_string_diff is undefined function. Anyone of you knows what extensions should be loaded to make this function available? Or if you can suggest another approach it will be great and much appreciated. :)
<?php
$old_article = "grey tiles 100 X 200 kitchen flooring";
$new_article = "grey tiles 300 X 600 kitchen flooring"
$diff = xdiff_string_diff($old_article, $new_article, 1);
if (is_string($diff)) {
echo "Differences between two articles:\n";
echo $diff;
}
?>
The strcmp() function compares two strings. Note: The strcmp() function is binary-safe and case-sensitive. Tip: This function is similar to the strncmp() function, with the difference that you can specify the number of characters from each string to be used in the comparison with strncmp().
You should not use == (equality operator) to compare these strings because they compare the reference of the string, i.e. whether they are the same object or not. On the other hand, equals() method compares whether the value of the strings is equal, and not the object itself.
The compare() function compares two strings and returns the following values according to the matching cases: Returns 0, if both the strings are the same. Returns <0, if the value of the character of the first string is smaller as compared to the second string input.
== Operator: This operator is used to check the given values are equal or not. If yes, it returns true, otherwise it returns false. Syntax: operand1 == operand2. === Operator: This operator is used to check the given values and its data type are equal or not.
You will have to install xdiff
pecl install xdiff
Alternatively you could try using array_diff to find differences between the strings. Explode them on space and then array_diff them:
$a1 = explode(" " , 'grey tiles 100 X 200 kitchen flooring');
$a2 = explode(" ", 'grey tiles 300 X 600 kitchen flooring');
echo join(' ', array_diff($a1, $a2)); // 100 200
This will not catch the X though, as the X is going to be the common part of the string in that context.
On every manual page of every function (or library) it always mentions if it's shipped with PHP by default or not and from what version of PHP they started packing it into the release.
If it's not in the release then it mentions the package it's in (either PECL or PEAR).
O its manual page it states:
PECL xdiff >= 0.2.0
If you're running BSD/Solaris/Linux/Mac all you have to do is open a console and type in:
pear install xdiff
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With