Link to home
Start Free TrialLog in
Avatar of rgb192
rgb192Flag for United States of America

asked on

php difference in strings

$string1='hello, how are you'
$string2='very different'
$string3='hi, how are you'
$string4='another string'


using php, is there a way to compare $string1 against $string2, $string3, $string4 and see that $string1 is similar to $string3
Avatar of Slimshaneey
Slimshaneey
Flag of United Kingdom of Great Britain and Northern Ireland image

You can use the similar_text() function, or use more involved ones like levenshtein algorithm which gives the edit distance (The number of edits needed to make a string the same).


Others include soundex(): Returns the four-character soundex key of a word, which should be the same as the key for any similar-sounding word.
metaphone(): Similar to soundex, and possibly more effective for you. It's more accurate than soundex() as it knows the basic rules of English pronunciation. The metaphone generated keys are of variable length.
Here is the definition for similar_text():

http://php.net/manual/en/function.similar-text.php
Avatar of rinfo
rinfo

Firstly it is need to be cleared as to what he means by similar.
Is it similar sounding - soundex has to be used , or is string a lot like compared string - similarText  would get a percent of similarity. Or does he simply means string1 is equal to
string3.
ASKER CERTIFIED SOLUTION
Avatar of Ray Paseur
Ray Paseur
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Note the case-sensitive comparisons.  You might want to normalize to upper case.   Just a thought... ~Ray
Avatar of rgb192

ASKER

this code sample answered my question completely and taught me well
thanks


I have a similar question
gathering data from mysql instead of $string[]

https://www.experts-exchange.com/questions/27840917/mysql-instead-of-string.html