-
Notifications
You must be signed in to change notification settings - Fork 3
/
markdown-parser.php
50 lines (47 loc) · 1.55 KB
/
markdown-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
<?php
$blogPostArray = array();
try {
$fp = fopen("./sample.txt", "r");
} catch (Exception $e) {
echo 'File does not exist';
}
$hrTagCount = 0;
$readmoreCount = 0;
while ($contents = fgets($fp)) {
// echo $contents;
// echo substr($contents, 0, strpos($contents, ":"))."\n";
$contents = trim($contents);
if ($contents == '---') {
$hrTagCount++;
continue;
} else {
if ($hrTagCount < 2) {
$arrKey = strval(substr($contents, 0, strpos($contents, ":")));
$arrContent = filter_var(strval(str_replace('"', "", substr($contents, strpos($contents, ":")))), FILTER_SANITIZE_STRIPPED);
} else {
if (strtolower($contents) == "readmore") {
$readmoreCount++;
continue;
} elseif ($contents == "") {
continue;
} else {
if ($readmoreCount >= 1) {
$arrKey = "content";
$contents = str_replace("#", "", $contents);
$contents = trim($contents);
if (array_key_exists("content", $blogPostArray)) {
$arrContent = $blogPostArray["content"] ."\n". $contents;
} else {
$arrContent = $contents;
}
} else {
$arrKey = "short-content";
$arrContent = $contents;
}
}
}
$blogPostArray[$arrKey] = $arrContent;
}
}
fclose($fp);
print_r(json_encode($blogPostArray));