Skip to content
Advertisement

How to organzie data from a raw txt file with PHP [closed]

I have a raw data into a .txt file which i want to use to build a character stats out of it. Now sure if it will parse well below but i have into a textarea on my website (will leave a link below) where it is structured better than what it looks like here. Any ideas how can i put it to use with PHP? I want to use this data on a “character calculator” where people will choose a level and i want to dispaly the HP, Def, Str, Dex and all the other stats based on the level chosen. They are on the same row as the level so i was thinking either to read the level and see what’s on the row next or maybe a whole different approach. I am open to ideas

https://www.diablo-2.net/articles/mercenaries#act-1-rogues

Lvl HP  Def Str Dex AR  MinD    MaxD    Res
1   40  10  32  41  205 1   2   0
2   40  17  33  43  215 1   2   0
3   45  26  35  45  235 1   3   0
4   54  34  36  47  257 1   3   2
5   63  43  37  49  279 1   3   4
6   72  51  38  51  301 1   3   6
7   81  60  40  53  323 2   4   8
8   90  68  41  55  345 2   4   10
9   99  77  42  57  367 2   4   12
10  108 85  43  59  389 2   4   14
11  117 94  45  61  411 3   5   16
12  126 102 46  63  433 3   5   18
13  135 111 47  65  455 3   5   20
14  144 119 48  67  477 3   5   22
15  153 128 50  69  499 4   6   24
16  162 136 51  71  521 4   6   26
17  171 145 52  73  543 4   6   28
18  180 153 53  75  565 4   6   30
19  189 162 55  77  587 5   7   32
20  198 170 56  79  609 5   7   34
21  207 179 57  81  631 5   7   36
22  216 187 58  83  653 5   7   38
23  225 196 60  85  675 6   8   40
24  234 204 61  87  697 6   8   42
25  243 213 62  89  719 6   8   44
26  252 221 63  91  741 6   8   46
27  261 230 65  93  763 7   9   48
28  270 238 66  95  785 7   9   50
29  279 247 67  97  807 7   9   52
30  288 255 68  99  829 7   9   54
31  297 264 70  101 851 8   10  56
32  306 272 71  103 873 8   10  58
33  315 281 72  105 895 8   10  60
34  324 289 73  107 917 8   10  62
35  333 298 75  109 939 9   11  64
36  342 306 77  111 961 9   11  66
37  360 322 78  113 995 9   11  67
38  378 337 79  115 1029    10  12  69
39  396 353 80  117 1063    10  12  71
40  414 368 82  119 1097    11  13  73
41  432 384 83  121 1131    11  13  74
42  450 399 84  123 1165    12  14  76
43  468 415 85  125 1199    12  14  78
44  486 430 87  127 1233    13  15  80
45  504 446 88  129 1267    13  15  81
46  522 461 89  131 1301    14  16  83
47  540 477 90  133 1335    14  16  85
48  558 492 92  135 1369    15  17  87
49  576 508 93  137 1403    15  17  88
50  594 523 94  139 1437    16  18  90
51  612 539 95  141 1471    16  18  92
52  630 554 97  143 1505    17  19  94
53  648 570 98  145 1539    17  19  95
54  666 585 99  147 1573    18  20  97
55  684 601 100 149 1607    18  20  99
56  702 616 102 151 1641    19  21  101
57  720 632 103 153 1675    19  21  102
58  738 647 104 155 1709    20  22  104
59  756 663 105 157 1743    20  22  106
60  774 678 107 159 1777    21  23  108
61  792 694 108 161 1811    21  23  109
62  810 709 109 163 1845    22  24  111
63  828 725 110 165 1879    22  24  113
64  846 740 112 167 1913    23  25  115
65  864 756 113 169 1947    23  25  116
66  882 771 114 171 1981    24  26  118
67  900 787 116 173 2015    25  27  121
68  930 809 117 175 2061    25  27  122
69  960 832 118 177 2107    26  28  123
70  990 854 119 179 2153    27  29  124
71  1020    877 121 181 2199    28  30  126
72  1050    899 122 183 2245    28  30  127
73  1080    922 123 185 2291    29  31  128
74  1110    944 124 187 2337    30  32  129
75  1140    967 126 189 2383    31  33  131
76  1170    989 127 191 2429    31  33  132
77  1200    1012    128 193 2475    32  34  133
78  1230    1034    129 195 2521    33  35  134
79  1260    1057    131 197 2567    34  36  136
80  1290    1079    132 199 2613    34  36  137
81  1320    1102    133 201 2659    35  37  138
82  1350    1124    134 203 2705    36  38  139
83  1380    1147    136 205 2751    37  39  141
84  1410    1169    137 207 2797    37  39  142
85  1440    1192    138 209 2843    38  40  143
86  1470    1214    139 211 2889    39  41  144
87  1500    1237    141 213 2935    40  42  146
88  1530    1259    142 215 2981    40  42  147
89  1560    1282    143 217 3027    41  43  148
90  1590    1304    144 219 3073    42  44  149
91  1620    1327    146 221 3119    43  45  151
92  1650    1349    147 223 3165    43  45  152
93  1680    1372    148 225 3211    44  46  153
94  1710    1394    149 227 3257    45  47  154
95  1740    1417    151 229 3303    46  48  155
96  1770    1439    152 231 3349    46  48  157
97  1800    1462    153 233 3395    47  49  158
98  1830    1484    154 235 3441    48  50  159
Lvl HP  Def Str Dex AR  MinD    MaxD    Res

Edit: I checked the suggested idea by E. Safa Tok and I did some tests using this:

$level = 98;
echo "<table style='width: 360px; border-collapse: collapse; border: 1px solid black;'>";
echo "<tbody>";
foreach ($line as $key=>$value) {
    echo "<tr>";
    echo "<td>" . $key . "</td>";
    echo "<td>" . $result[$level][$key] . "</td>";
    echo "</tr>";
}
echo "</table>";

It works!!!! Thanks a lot.

Advertisement

Answer

I wrote a piece of code to split your input to array. After that process, also you can generate JSON formatted array with json_decode function.

<?php

$input = <<<EOT
Lvl HP  Def Str Dex AR  MinD    MaxD    Res
1   40  10  32  41  205 1   2   0
2   40  17  33  43  215 1   2   0
3   45  26  35  45  235 1   3   0
4   54  34  36  47  257 1   3   2
5   63  43  37  49  279 1   3   4
6   72  51  38  51  301 1   3   6
7   81  60  40  53  323 2   4   8
8   90  68  41  55  345 2   4   10
9   99  77  42  57  367 2   4   12
10  108 85  43  59  389 2   4   14
11  117 94  45  61  411 3   5   16
12  126 102 46  63  433 3   5   18
13  135 111 47  65  455 3   5   20
14  144 119 48  67  477 3   5   22
15  153 128 50  69  499 4   6   24
16  162 136 51  71  521 4   6   26
17  171 145 52  73  543 4   6   28
18  180 153 53  75  565 4   6   30
19  189 162 55  77  587 5   7   32
20  198 170 56  79  609 5   7   34
21  207 179 57  81  631 5   7   36
22  216 187 58  83  653 5   7   38
23  225 196 60  85  675 6   8   40
24  234 204 61  87  697 6   8   42
25  243 213 62  89  719 6   8   44
26  252 221 63  91  741 6   8   46
27  261 230 65  93  763 7   9   48
28  270 238 66  95  785 7   9   50
29  279 247 67  97  807 7   9   52
30  288 255 68  99  829 7   9   54
31  297 264 70  101 851 8   10  56
32  306 272 71  103 873 8   10  58
33  315 281 72  105 895 8   10  60
34  324 289 73  107 917 8   10  62
35  333 298 75  109 939 9   11  64
36  342 306 77  111 961 9   11  66
37  360 322 78  113 995 9   11  67
38  378 337 79  115 1029    10  12  69
39  396 353 80  117 1063    10  12  71
40  414 368 82  119 1097    11  13  73
41  432 384 83  121 1131    11  13  74
42  450 399 84  123 1165    12  14  76
43  468 415 85  125 1199    12  14  78
44  486 430 87  127 1233    13  15  80
45  504 446 88  129 1267    13  15  81
46  522 461 89  131 1301    14  16  83
47  540 477 90  133 1335    14  16  85
48  558 492 92  135 1369    15  17  87
49  576 508 93  137 1403    15  17  88
50  594 523 94  139 1437    16  18  90
51  612 539 95  141 1471    16  18  92
52  630 554 97  143 1505    17  19  94
53  648 570 98  145 1539    17  19  95
54  666 585 99  147 1573    18  20  97
55  684 601 100 149 1607    18  20  99
56  702 616 102 151 1641    19  21  101
57  720 632 103 153 1675    19  21  102
58  738 647 104 155 1709    20  22  104
59  756 663 105 157 1743    20  22  106
60  774 678 107 159 1777    21  23  108
61  792 694 108 161 1811    21  23  109
62  810 709 109 163 1845    22  24  111
63  828 725 110 165 1879    22  24  113
64  846 740 112 167 1913    23  25  115
65  864 756 113 169 1947    23  25  116
66  882 771 114 171 1981    24  26  118
67  900 787 116 173 2015    25  27  121
68  930 809 117 175 2061    25  27  122
69  960 832 118 177 2107    26  28  123
70  990 854 119 179 2153    27  29  124
71  1020    877 121 181 2199    28  30  126
72  1050    899 122 183 2245    28  30  127
73  1080    922 123 185 2291    29  31  128
74  1110    944 124 187 2337    30  32  129
75  1140    967 126 189 2383    31  33  131
76  1170    989 127 191 2429    31  33  132
77  1200    1012    128 193 2475    32  34  133
78  1230    1034    129 195 2521    33  35  134
79  1260    1057    131 197 2567    34  36  136
80  1290    1079    132 199 2613    34  36  137
81  1320    1102    133 201 2659    35  37  138
82  1350    1124    134 203 2705    36  38  139
83  1380    1147    136 205 2751    37  39  141
84  1410    1169    137 207 2797    37  39  142
85  1440    1192    138 209 2843    38  40  143
86  1470    1214    139 211 2889    39  41  144
87  1500    1237    141 213 2935    40  42  146
88  1530    1259    142 215 2981    40  42  147
89  1560    1282    143 217 3027    41  43  148
90  1590    1304    144 219 3073    42  44  149
91  1620    1327    146 221 3119    43  45  151
92  1650    1349    147 223 3165    43  45  152
93  1680    1372    148 225 3211    44  46  153
94  1710    1394    149 227 3257    45  47  154
95  1740    1417    151 229 3303    46  48  155
96  1770    1439    152 231 3349    46  48  157
97  1800    1462    153 233 3395    47  49  158
98  1830    1484    154 235 3441    48  50  159
Lvl HP  Def Str Dex AR  MinD    MaxD    Res
EOT;

// Split to lines
$lines = explode("n", $input);
$result = array();

// for each line
foreach ($lines as $line) {
    // parse with space char
    $parsed_line = explode(" ", $line);

    $index = 0;
    foreach ($parsed_line as $i) {
        if($i == " " or strlen($i)==0){
            array_splice($parsed_line, $index, 1);
            // if item popped, reduce index
            $index -= 1;
        }
        $index += 1;
    }
    // create array of line to add result array.
    $line = array(
        "Lvl" => $parsed_line[0],
        "HP" => $parsed_line[1],
        "Def"   => $parsed_line[2],
        "Str"  => $parsed_line[3],
        "Dex" => $parsed_line[4],
        "AR" => $parsed_line[5],
        "MinD" => $parsed_line[6],
        "MaxD" => $parsed_line[7],
        "Res" => $parsed_line[8],
    );
    array_push($result, $line);
}
// can view whole array with:
//var_dump($result);

// Example of usage of item selection method 9 -> Lvl. Def -> parameter.
echo $result[9]['Def'];
?>
User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement