|
1
|
|
|
<?php namespace Gears\String\Methods; |
|
2
|
|
|
//////////////////////////////////////////////////////////////////////////////// |
|
3
|
|
|
// __________ __ ________ __________ |
|
4
|
|
|
// \______ \ |__ ______ / _____/ ____ _____ ______\______ \ _______ ___ |
|
5
|
|
|
// | ___/ | \\____ \/ \ ____/ __ \\__ \\_ __ \ | _// _ \ \/ / |
|
6
|
|
|
// | | | Y \ |_> > \_\ \ ___/ / __ \| | \/ | ( <_> > < |
|
7
|
|
|
// |____| |___| / __/ \______ /\___ >____ /__| |______ /\____/__/\_ \ |
|
8
|
|
|
// \/|__| \/ \/ \/ \/ \/ |
|
9
|
|
|
// ----------------------------------------------------------------------------- |
|
10
|
|
|
// Designed and Developed by Brad Jones <brad @="bjc.id.au" /> |
|
11
|
|
|
// ----------------------------------------------------------------------------- |
|
12
|
|
|
//////////////////////////////////////////////////////////////////////////////// |
|
13
|
|
|
|
|
14
|
|
|
use voku\helper\UTF8; |
|
15
|
|
|
|
|
16
|
|
|
trait LongestCommon |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* Returns the longest common prefix between the string and $otherStr. |
|
20
|
|
|
* |
|
21
|
|
|
* @param string $otherStr Second string for comparison |
|
22
|
|
|
* |
|
23
|
|
|
* @return static String being the longest common prefix |
|
24
|
|
|
*/ |
|
25
|
|
|
public function longestCommonPrefix($otherStr) |
|
26
|
|
|
{ |
|
27
|
|
|
$longestCommonPrefix = ''; |
|
28
|
|
|
|
|
29
|
|
|
for ($i = 0; $i < min($this->getLength(), UTF8::strlen($otherStr, $this->encoding)); $i++) |
|
30
|
|
|
{ |
|
31
|
|
|
$char = UTF8::substr($this->scalarString, $i, 1, $this->encoding); |
|
32
|
|
|
|
|
33
|
|
|
if ($char == UTF8::substr($otherStr, $i, 1, $this->encoding)) |
|
34
|
|
|
{ |
|
35
|
|
|
$longestCommonPrefix .= $char; |
|
36
|
|
|
} |
|
37
|
|
|
else |
|
38
|
|
|
{ |
|
39
|
|
|
break; |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
return $this->newSelf($longestCommonPrefix); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Returns the longest common suffix between the string and $otherStr. |
|
48
|
|
|
* |
|
49
|
|
|
* @param string $otherStr Second string for comparison |
|
50
|
|
|
* |
|
51
|
|
|
* @return static String being the longest common suffix |
|
52
|
|
|
*/ |
|
53
|
|
|
public function longestCommonSuffix($otherStr) |
|
54
|
|
|
{ |
|
55
|
|
|
$longestCommonSuffix = ''; |
|
56
|
|
|
|
|
57
|
|
|
for ($i = 1; $i <= min($this->getLength(), UTF8::strlen($otherStr, $this->encoding)); $i++) |
|
58
|
|
|
{ |
|
59
|
|
|
$char = UTF8::substr($this->scalarString, -$i, 1, $this->encoding); |
|
60
|
|
|
|
|
61
|
|
|
if ($char == UTF8::substr($otherStr, -$i, 1, $this->encoding)) |
|
62
|
|
|
{ |
|
63
|
|
|
$longestCommonSuffix = $char . $longestCommonSuffix; |
|
64
|
|
|
} |
|
65
|
|
|
else |
|
66
|
|
|
{ |
|
67
|
|
|
break; |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $this->newSelf($longestCommonSuffix); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Returns the longest common substring between the string and $otherStr. |
|
76
|
|
|
* In the case of ties, it returns that which occurs first. |
|
77
|
|
|
* |
|
78
|
|
|
* @param string $otherStr Second string for comparison |
|
79
|
|
|
* |
|
80
|
|
|
* @return static String being the longest common substring |
|
81
|
|
|
*/ |
|
82
|
|
|
public function longestCommonSubstring($otherStr) |
|
83
|
|
|
{ |
|
84
|
|
|
// Uses dynamic programming to solve |
|
85
|
|
|
// http://en.wikipedia.org/wiki/Longest_common_substring_problem |
|
86
|
|
|
|
|
87
|
|
|
$strLength = $this->getLength(); |
|
88
|
|
|
$otherLength = UTF8::strlen($otherStr, $this->encoding); |
|
89
|
|
|
|
|
90
|
|
|
// Return if either string is empty |
|
91
|
|
|
if ($strLength == 0 || $otherLength == 0) return $this->newSelf(''); |
|
92
|
|
|
|
|
93
|
|
|
$len = 0; $end = 0; |
|
94
|
|
|
$table = array_fill(0, $strLength + 1, array_fill(0, $otherLength + 1, 0)); |
|
95
|
|
|
|
|
96
|
|
|
for ($i = 1; $i <= $strLength; $i++) |
|
97
|
|
|
{ |
|
98
|
|
|
for ($j = 1; $j <= $otherLength; $j++) |
|
99
|
|
|
{ |
|
100
|
|
|
$strChar = UTF8::substr($this->scalarString, $i - 1, 1, $this->encoding); |
|
101
|
|
|
$otherChar = UTF8::substr($otherStr, $j - 1, 1, $this->encoding); |
|
102
|
|
|
|
|
103
|
|
|
if ($strChar == $otherChar) |
|
104
|
|
|
{ |
|
105
|
|
|
$table[$i][$j] = $table[$i - 1][$j - 1] + 1; |
|
106
|
|
|
|
|
107
|
|
|
if ($table[$i][$j] > $len) |
|
108
|
|
|
{ |
|
109
|
|
|
$len = $table[$i][$j]; |
|
110
|
|
|
$end = $i; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
else |
|
114
|
|
|
{ |
|
115
|
|
|
$table[$i][$j] = 0; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return $this->newSelf(UTF8::substr |
|
121
|
|
|
( |
|
122
|
|
|
$this->scalarString, |
|
123
|
|
|
$end - $len, |
|
124
|
|
|
$len, |
|
125
|
|
|
$this->encoding |
|
126
|
|
|
)); |
|
127
|
|
|
} |
|
128
|
|
|
} |
|
129
|
|
|
|