-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrecipe.php
167 lines (151 loc) · 5.25 KB
/
recipe.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
$postIngredients = $_POST['ingredients'];
$postSteps = $_POST['steps'];
$ingredients = explode("\n", $postIngredients);
$steps = explode("\n", $postSteps);
$lineLength = 36;
// echo $ingredients[0];
// echo $steps[0];
//MC
//Format ingredients and steps to fit onto display
$ingredientHeader = " Ingredients";
$allIngredientPages = array();
$ingredientPage = array();
$ingredientPage[] = $ingredientHeader;
for ($i = 0; $i < count($ingredients); $i++) {
// $ingredients[$i] = utf8_decode($ingredients[$i]);
$ingredients[$i] = str_replace("\n", '', $ingredients[$i]);
$ingredients[$i] = str_replace("\\", '', $ingredients[$i]);
$ingredients[$i] = str_replace("®", "", $ingredients[$i]);
// $ingredients[$i] = str_replace("\\u00a0", "", $ingredients[$i]);
// echo $ingredients[$i];
$lineCnt = (int) ((mb_strlen($ingredients[$i]) / $lineLength) + 1);
$words = explode(' ', $ingredients[$i]);
array_filter($words);
// array_unshift($words, "" . ($i + 1) . ". ");
for ($j = 0; $j < $lineCnt; ++$j) {
$line = "";
while (count($words) > 0 && mb_strlen($line . $words[0]) <= $lineLength) {
$line = $line . array_shift($words) . ' ';
}
//If line is not empty, add it.
if (!empty(trim($line))) {
$ingredientPage [] = trim($line);
}
}
if (count($ingredientPage) >= 11 || ($i == count($ingredients) - 1)) {
$allIngredientPages[] = $ingredientPage;
$ingredientPage = array();
$ingredientPage[] = $ingredientHeader;
}
}
$allStepPages = array();
$stepPage = array();
$stepHeader = " Steps ";
$stepPage[] = $stepHeader;
for ($i = 0; $i < count($steps); $i++) {
$steps[$i] = str_replace("\n", '', $steps[$i]);
$steps[$i] = str_replace("\\", '', $steps[$i]);
$steps[$i] = str_replace('"', '"', $steps[$i]);
$ingredients[$i] = str_replace("®", '', $ingredients[$i]);
$lineCnt = (int) ((mb_strlen($steps[$i]) / $lineLength) + 1);
$words = explode(' ', $steps[$i]);
$words = str_replace(' ', '', $words);
array_filter($words);
// array_unshift($words, "" . ($i + 1) . ". ");
for ($j = 0; $j < $lineCnt; ++$j) {
$line = "";
while (count($words) > 0 && mb_strlen($line . $words[0]) <= $lineLength) {
$line = $line . array_shift($words) . ' ';
}
if (!empty(trim($line))) {
$stepPage [] = $line;
}
if (count($stepPage) >= 11) {
$allStepPages[] = $stepPage;
$stepPage = array();
$stepPage[] = $stepHeader;
}
}
if ($i == (count($steps) - 1)) {
$allStepPages[] = $stepPage;
$stepPage = array();
$stepPage[] = $stepHeader;
}
}
$ingredients = array_filter($allIngredientPages);
$steps = array_filter($allStepPages);
$arr["Pages"] = array_merge($ingredients, $steps);
for ($i = 0; $i < count($arr["Pages"]); ++$i) {
$pageIndex = ($i + 1) . "/" . count($arr["Pages"]);
$numLines = count($arr["Pages"][$i]);
// echo $numLines . " ";
// echo "here";
for ($j = 0; (11 - ($numLines + $j)) > 0; ++$j) {
$arr["Pages"][$i][] = " ";
}
$arr["Pages"][$i][] = str_repeat(" ", 91 - strlen($pageIndex)) . $pageIndex;
}
// $allIngredientPages = array();
// $ingredientPage = array();
// for ($i = 0; $i < count($ingredients); $i++) {
// $ingredients[$i] = str_replace("\n", '', $ingredients[$i]);
// $ingredients[$i] = str_replace("\\", '', $ingredients[$i]);
//
// $lineCnt = (int) ((mb_strlen($ingredients[$i]) / $lineLength) + 1);
//
// $words = explode(' ', $ingredients[$i]);
//
// for ($j = 0; $j < $lineCnt; ++$j) {
// $line = "";
// while (count($words) > 0 && mb_strlen($line . $words[0]) <= $lineLength) {
// $line = $line . array_shift($words) . ' ';
// }
// $ingredientPage [] = $line;
// if (count($ingredientPage) >= 8) {
// $allIngredientPages[] = $ingredientPage;
// $ingredientPage = array();
// }
// }
//
// if (($i == count($ingredients) - 1)) {
// $allIngredientPages[] = $ingredientPage;
// $ingredientPage = array();
// }
// }
//
// $allStepPages = array();
// $stepPage = array();
// for ($i = 0; $i < count($steps); $i++) {
// $steps[$i] = str_replace("\n", '', $steps[$i]);
// $steps[$i] = str_replace("\\", '', $steps[$i]);
// $lineCnt = (int) ((mb_strlen($steps[$i]) / $lineLength) + 1);
//
// $words = explode(' ', $steps[$i]);
//
// for ($j = 0; $j < $lineCnt; ++$j) {
// if (count($stepPage) >= 8) {
// $allStepPages[] = $stepPage;
// $stepPage = array();
// }
// $line = "";
// while (count($words) > 0 && mb_strlen($line . $words[0]) <= $lineLength) {
// $line = $line . array_shift($words) . ' ';
// }
// if ($line != "") {
// $stepPage [] = $line;
// }
//
// }
//
// if (($i == (count($steps) - 1)) {
// $allStepPages[] = $stepPage;
// $stepPage = array();
// }
// }
// $ingredients = array_filter($allIngredientPages);
// $steps = array_filter($allStepPages);
// $arr["Pages"] = array_merge($ingredients, $steps);
file_put_contents('recipe.json', json_encode($arr));
echo json_encode($arr);
?>