forked from marshallbrekka/WebBot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringSearch 2.php
127 lines (99 loc) · 2.6 KB
/
StringSearch 2.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
<?php
require_once 'DOMParser.php';
require_once 'StringIndexer.php';
/**
* StringSearch
*
* @author Marshall
*/
class StringSearch {
private $stringIndex;
public function __construct($string) {
$this->stringIndex = new StringIndexer();
$this->stringIndex->indexString($string);
}
/**
*
* @param string $string
* @return bool
*/
public function isStringPresent($string) {
$words = explode(' ', $string);
$wordPositions = array();
foreach($words as $word) {
$wordPositions[$word] = $this->stringIndex->getIndexArrayOfWord($word);
if(!$wordPositions[$word]) {
return false;
}
}
foreach($wordPositions[$words[0]] as $position) {
$continue = true;
for($i = 1; $i < count($wordPositions) && $continue; $i++) {
$wordIsPresent = false;
for($y = 0; $y < count($wordPositions[$words[$i]]); $y++) {
if($position + $i == $wordPositions[$words[$i]][$y]) {
$wordIsPresent = true;
}
}
if(!$wordIsPresent) {
$continue = false;
}
}
}
return true;
}
/**
*
* @param string $string string to match, %p is replaced with the pattern
* @param string $pattern regex pattern
* @return string matched pattern
*/
public function searchContainingPattern($string, $pattern) {
echo "********\n\nsearch started\n\n\n";
$words = explode(' ', $string);
$wordPositions = array();
$patternIndex = array_search('%p', $words);
print_r($words);
$matchingIndexs = array();
for($t = 0; $t < count($words); $t++) {
if($t != $patternIndex) {
$wordPositions[$words[$t]] = $this->stringIndex->getIndexArrayOfWord($words[$t]);
print_r($wordPositions[$words[$t]]);
echo "\n****\n post print\n";
if(!$wordPositions[$words[$t]]) {
return false;
}
}
}
print_r($wordPositions);
for($z = 0; $z < count($wordPositions[$words[0]]); $z++) {
$continue = true;
for($i = 1; $i < count($wordPositions) && $continue; $i++) {
if(!$i == $patternIndex) {
$wordIsPresent = false;
for($y = 0; $y < count($wordPositions[$words[$i]]); $y++) {
if($wordPositions[$words[0]][$z] + $i == $wordPositions[$words[$i]][$y]) {
$wordIsPresent = true;
}
}
if(!$wordIsPresent) {
$continue = false;
}
}
}
if($continue) {
$matchingIndexs[] = $z;
}
}
print_r($matchingIndexs);
foreach($matchingIndexs as $index) {
$string = $this->stringIndex->getWordAtIndex($wordPositions[$words[0]][$index] + $patternIndex);
if(preg_match($pattern, $string)) {
$words[$patternIndex] = $string;
return implode(' ', $words);
}
}
return false;
}
}
?>