-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrleEncode.php
More file actions
76 lines (57 loc) · 1.65 KB
/
rleEncode.php
File metadata and controls
76 lines (57 loc) · 1.65 KB
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
<?php
class Encode {
private $string;
private $flag;
public function __construct($string, $flag) {
$this->string = $string;
$this->flag = $flag;
}
public function encode() {
if($this->shouldEncode()) {
$code = $this->string;
$encode = '';
for ($i = 0; $i < strlen($code); $i++){
$currentChar = $code[$i] ;
$count = 1 ;
for ($b = $i; $b < strlen($code); $b++){
if ($code[$b + 1] != $currentChar) break ;
$count++ ;
$i++ ;
}
$encode .= $count . $currentChar ;
}
$this->string = $encode;
$this->flag = 0;
}
}
public function decode() {
if(!($this->shouldEncode())) {
$code = $this->string;
$encode = '';
for($i = 0; $i < strlen($code); $i++) {
if($i % 2) {
continue;
}
for($b = 1; $b <= $code[$i]; $b++) {
$encode .= $code[$i + 1];
}
}
$this->string = $encode;
$this->flag = 1;
}
}
public function getText() {
return $this->string;
}
private function shouldEncode() {
return $this->flag == 1 ? true : false;
}
}
$string = new Encode('ffafhfthtrftttt jjjtyjyt jyjfgyyfyfyff', 1);
echo $string->getText();
echo '<br>';
$string->encode();
echo $string->getText();
echo '<br>';
$string->decode();
echo $string->getText();