This repository has been archived by the owner on Feb 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
bbcode.php
160 lines (114 loc) · 5.39 KB
/
bbcode.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
<?php
/**
* BBCode to HTML converter
*
* Created by Kai Mallea ([email protected])
*
* Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
*/
class BBCode {
protected $bbcode_table = array();
public function __construct () {
// Replace [b]...[/b] with <strong>...</strong>
$this->bbcode_table["/\[b\](.*?)\[\/b\]/is"] = function ($match) {
return "<strong>$match[1]</strong>";
};
// Replace [i]...[/i] with <em>...</em>
$this->bbcode_table["/\[i\](.*?)\[\/i\]/is"] = function ($match) {
return "<em>$match[1]</em>";
};
// Replace [code]...[/code] with <pre><code>...</code></pre>
$this->bbcode_table["/\[code\](.*?)\[\/code\]/is"] = function ($match) {
return "<pre><code>$match[1]</code></pre>";
};
// Replace [quote]...[/quote] with <blockquote><p>...</p></blockquote>
$this->bbcode_table["/\[quote\](.*?)\[\/quote\]/is"] = function ($match) {
return "<blockquote><p>$match[1]</p></blockquote>";
};
// Replace [quote="person"]...[/quote] with <blockquote><p>...</p></blockquote>
$this->bbcode_table["/\[quote=\"([^\"]+)\"\](.*?)\[\/quote\]/is"] = function ($match) {
return "$match[1] wrote: <blockquote><p>$match[2]</p></blockquote>";
};
// Replace [size=30]...[/size] with <span style="font-size:30%">...</span>
$this->bbcode_table["/\[size=(\d+)\](.*?)\[\/size\]/is"] = function ($match) {
return "<span style=\"font-size:$match[1]%\">$match[2]</span>";
};
// Replace [s] with <del>
$this->bbcode_table["/\[s\](.*?)\[\/s\]/is"] = function ($match) {
return "<del>$match[1]</del>";
};
// Replace [u]...[/u] with <span style="text-decoration:underline;">...</span>
$this->bbcode_table["/\[u\](.*?)\[\/u\]/is"] = function ($match) {
return '<span style="text-decoration:underline;">' . $match[1] . '</span>';
};
// Replace [center]...[/center] with <div style="text-align:center;">...</div>
$this->bbcode_table["/\[center\](.*?)\[\/center\]/is"] = function ($match) {
return '<div style="text-align:center;">' . $match[1] . '</div>';
};
// Replace [color=somecolor]...[/color] with <span style="color:somecolor">...</span>
$this->bbcode_table["/\[color=([#a-z0-9]+)\](.*?)\[\/color\]/is"] = function ($match) {
return '<span style="color:'. $match[1] . ';">' . $match[2] . '</span>';
};
// Replace [email]...[/email] with <a href="mailto:...">...</a>
$this->bbcode_table["/\[email\](.*?)\[\/email\]/is"] = function ($match) {
return "<a href=\"mailto:$match[1]\">$match[1]</a>";
};
// Replace [[email protected]]An e-mail link[/email] with <a href="mailto:[email protected]">An e-mail link</a>
$this->bbcode_table["/\[email=(.*?)\](.*?)\[\/email\]/is"] = function ($match) {
return "<a href=\"mailto:$match[1]\">$match[2]</a>";
};
// Replace [url]...[/url] with <a href="...">...</a>
$this->bbcode_table["/\[url\](.*?)\[\/url\]/is"] = function ($match) {
return "<a href=\"$match[1]\">$match[1]</a>";
};
// Replace [url=http://www.google.com/]A link to google[/url] with <a href="http://www.google.com/">A link to google</a>
$this->bbcode_table["/\[url=(.*?)\](.*?)\[\/url\]/is"] = function ($match) {
return "<a href=\"$match[1]\">$match[2]</a>";
};
// Replace [img]...[/img] with <img src="..."/>
$this->bbcode_table["/\[img\](.*?)\[\/img\]/is"] = function ($match) {
return "<img src=\"$match[1]\"/>";
};
// Replace [list]...[/list] with <ul><li>...</li></ul>
$this->bbcode_table["/\[list\](.*?)\[\/list\]/is"] = function ($match) {
$match[1] = preg_replace_callback("/\[\*\]([^\[\*\]]*)/is", function ($submatch) {
return "<li>" . preg_replace("/[\n\r?]$/", "", $submatch[1]) . "</li>";
}, $match[1]);
return "<ul>" . preg_replace("/[\n\r?]/", "", $match[1]) . "</ul>";
};
// Replace [list=1|a]...[/list] with <ul|ol><li>...</li></ul|ol>
$this->bbcode_table["/\[list=(1|a)\](.*?)\[\/list\]/is"] = function ($match) {
if ($match[1] == '1') {
$list_type = '<ol>';
} else if ($match[1] == 'a') {
$list_type = '<ol style="list-style-type: lower-alpha">';
} else {
$list_type = '<ol>';
}
$match[2] = preg_replace_callback("/\[\*\]([^\[\*\]]*)/is", function ($submatch) {
return "<li>" . preg_replace("/[\n\r?]$/", "", $submatch[1]) . "</li>";
}, $match[2]);
return $list_type . preg_replace("/[\n\r?]/", "", $match[2]) . "</ol>";
};
// Replace [youtube]...[/youtube] with <iframe src="..."></iframe>
$this->bbcode_table["/\[youtube\](?:http?:\/\/)?(?:www\.)?youtu(?:\.be\/|be\.com\/watch\?v=)([A-Z0-9\-_]+)(?:&(.*?))?\[\/youtube\]/i"] = function ($match) {
return "<iframe class=\"youtube-player\" type=\"text/html\" width=\"640\" height=\"385\" src=\"http://www.youtube.com/embed/$match[1]\" frameborder=\"0\"></iframe>";
};
}
public function toHTML ($str, $escapeHTML=false, $nr2br=false) {
if (!$str) {
return "";
}
if ($escapeHTML) {
$str = htmlspecialchars($str);
}
foreach($this->bbcode_table as $key => $val) {
$str = preg_replace_callback($key, $val, $str);
}
if ($nr2br) {
$str = preg_replace_callback("/\n\r?/", function ($match) { return "<br/>"; }, $str);
}
return $str;
}
}
?>