-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimpletablecode.php
More file actions
106 lines (91 loc) · 2.81 KB
/
simpletablecode.php
File metadata and controls
106 lines (91 loc) · 2.81 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
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
<?php
/**
* Simple [table] Code
* Copyright 2019 MINI² e.V.
* License: GNU General Public License v3.0
*
* Replaces [table] codes with HTML tables, uses new Lines to separate rows and Pipes (|) to separate cells
*/
// Disallow direct access to this file for security reasons
if(!defined("IN_MYBB"))
{
die("Direct initialization of this file is not allowed.");
}
function simpletablecode_info()
{
return array(
"name" => "Simple MyBB [table] Code",
"description" => "Replaces [table] codes with HTML tables, uses new Lines to separate rows and Pipes (|) to separate cells",
"website" => "https://www.mini2.info",
"author" => "A-Dalton (MINI²)",
"authorsite" => "https://github.com/A-Dalton",
"version" => "0.1",
"guid" => "",
"codename" => str_replace('.php', '', basename(__FILE__)),
"compatibility" => "*"
);
}
// Execute the 'tableCode_replacer' method whenever a message is parsed
$plugins->add_hook("parse_message", "tableCode_replacer");
function tableCode_replacer($message)
{
// $message = "Siehe hier:\n\n[table]H1|H2|[b]H3[/b]\nR1H1|R1H2\nR2H1||R1H3[/table] \n\n\nENDE";
$tableMatchRegex = "/\[table\].*?\[\/table\]|\[table=.*\].*?\[\/table\]/is";
$matchesCount = preg_match_all($tableMatchRegex, $message, $matches);
if ($matchesCount)
{
// Split contains everything outside the match (two empty strings if the entire string matches)
$split = preg_split($tableMatchRegex, $message);
// Used to count and re-combine the non-table parts of the message
$iSp = 0;
$newMessage = $split[$iSp++];
foreach ($matches[0] as $match)
{
$newMessage .= '<table class="tborder">';
$table = preg_replace("/\[table\](.*)\[\/table\]/is", "$1", $match);
// Jeder Eintrag in $tblRows ergibt eine '<tr>'
$iRow = 0;
$tblRows = explode("\n", $table);
foreach($tblRows as $tblRow)
{
if ($iRow == 0) {
$newMessage .= '<thead><tr class="thead">';
}
elseif ($iRow == 1) {
$newMessage .= '<tbody><tr>';
}
else {
$newMessage .= "<tr>";
}
foreach(explode("|", $tblRow) as $tblCell)
{
if ($iRow == 0){
$newMessage .= '<td style="padding: 5px">'.$tblCell.'</td>';
}
elseif ($iRow%2==1){
$newMessage .= '<td class="trow2">'.$tblCell.'</td>';
}
else{
$newMessage .= '<td class="trow1">'.$tblCell.'</td>';
}
}
if ($iRow == 0) {
$newMessage .= "</tr></thead>";
}
elseif (($iRow + 1) == count($tblRows)) {
$newMessage .= "</tr></tbody>";
}
else {
$newMessage .= "</tr>";
}
$iRow++;
}
$newMessage .= "</table>".$split[$iSp++];
}
return $newMessage;
}
else
{
return $message;
}
}