Takes a string and produces all possible substrings
public static getAllSubStrings ( string $text ) : array | ||
$text | string | |
Результат | array |
public function testAllSubstrings() { $text = 'abc'; $expected = ['a', 'ab', 'abc', 'b', 'bc', 'c']; $substrings = Text::getAllSubStrings($text); $this->assertCount(6, $substrings); $this->assertEquals($expected, $substrings); }
/** * Returns the Longest common substring * @param string $text1 * @param string $text2 * @return string */ public function similarity($text1, $text2) { $intersection = array_intersect(Text::getAllSubStrings($text1), Text::getAllSubStrings($text2)); $max = 0; $lcs = ''; foreach ($intersection as $substr) { if (strlen($substr) > $max) { $max = strlen($substr); $lcs = $substr; } } return $lcs; }
/** * Returns the Longest common substring * @param string $text1 * @param string $text2 * @return string */ public function similarity($text1, $text2) { if ($this->useCache && !isset($this->cache[$text2])) { $this->cache[$text2] = Text::getAllSubStrings($text2); } $intersection = array_intersect(Text::getAllSubStrings($text1), $this->useCache ? $this->cache[$text2] : Text::getAllSubStrings($text2)); $max = 0; $lcs = ''; foreach ($intersection as $substr) { $strlen = mb_strlen($substr); if ($strlen > $max) { $max = $strlen; $lcs = $substr; } } return $lcs; }