-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.php
61 lines (58 loc) · 1.36 KB
/
parser.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
<?php
class Parser {
var $parsed = array();
var $active_key = "";
public function validate($post_arr) {
if(isset($post_arr['job_description']) && !empty($post_arr['job_description'])) {
return array(
'status' => true
);
} else {
return array(
'status' => false,
'error_msg' => "Job Description is required."
);
}
}
public function doParse($post_arr) {
$description = $post_arr['job_description'];
$expl_desc = explode("\n", $description);
foreach($expl_desc as $line) {
$this->process($line);
}
return $this->parsed;
}
public function process($line) {
if(!empty($line)) {
if(strstr($line, ":")) {
$expl = explode(":", $line);
if(!empty($expl[0])) {
$this->active_key = str_replace(" ", "_", strtolower($expl[0]));
}
if(isset($expl[1]) && !empty($expl[1])) {
$this->parsed[$this->active_key] = trim($expl[1]);
} else {
$this->parsed[$this->active_key] = "";
}
} else {
$this->parsed[$this->active_key] .= trim($line);
}
}
}
}
$obj = new Parser();
$post_arr = isset($_POST) ? $_POST : array();
$res = $obj->validate($post_arr);
if($res['status']) {
$parsed_str = $obj->doParse($post_arr);
$res = array(
'status' => true,
'data' => $parsed_str
);
echo json_encode($res);
} else {
echo json_encode(
$res
);
}
?>