3v4l.org

run code in 300+ PHP versions simultaneously
<?php //http://stackoverflow.com/questions/653157/a-better-similarity-ranking-algorithm-for-variable-length-strings class StringMatch{ public static function compare($a, $b){ $pairsA = self::wordLetterPairs($a); $pairsB = self::wordLetterPairs($b); $union = count($pairsA) + count($pairsB); $intersect = count(array_intersect($pairsA, $pairsB)); return 2*$intersect/$union; } public static function letterPairs($str){ $pairCount = strlen($str)-1; //don't compute every time in if $pairs = array(); for($i = 0; $i < $pairCount; $i++){ $pairs[] = substr($str, $i, 2); } return $pairs; } public static function wordLetterPairs($str){ $allPairs = array(); $str = explode(" ", $str); //get pairs in each word foreach($str as $w){ $pairs = self::letterPairs($w); //make sure, no doubled pairs are included foreach($pairs as $p){ $allPairs[$p] = $p; } } return array_values($allPairs); } } var_dump(StringMatch::compare("GutenTag Frau Fickerin", "Ficker Herr GutenTag"));
Output for git.master, git.master_jit, rfc.property-hooks
float(0.7741935483870968)

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
58.02 ms | 401 KiB | 8 Q