note 18083 deleted from ref.strings by tomsommer

From: Date: Wed, 15 Sep 2004 20:49:08 +0000
Subject: note 18083 deleted from ref.strings by tomsommer
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-76797@lists.php.net to get a copy of this message
Note Submitter: carl at _nospam_youngbloods dot org ---- I've ported a function to PHP that will find the longest common substring in two different strings. Here it is: function LongestCommonSubstring($string1, $string2) { // find length of longest common substring first $L = array(); $length1 = strlen($string1); $length2 = strlen($string2); for ($i = $length1; $i >= 0; $i--) { for ($j = $length2; $j >= 0; $j--) { if ($string1[$i] == '' || $string2[$j] == '') $L[$i][$j] = 0; elseif ($string1[$i] == $string2[$j]) $L[$i][$j] = 1 + $L[$i + 1][$j + 1]; else $L[$i][$j] = max($L[$i + 1][$j], $L[$i][$j + 1]); } } // then find the actual substring $substring = ''; $i = 0; $j = 0; while ($i < $length1 && $j < $length2) { if ($string1[$i] == $string2[$j]) { $substring .= $string1[$i]; $i++; $j++; } elseif ($L[$i + 1][$j] >= $L[$i][$j + 1]) $i++; else $j++; } return $substring; } Here is where I found the original: http://www1.ics.uci.edu/~eppstein/161/960229.html

« previous php.notes (#76797) next »