1
function BBCode($Text)
2
{
3
// Replace any html brackets with HTML Entities to prevent executing HTML or script
4
// Don't use strip_tags here because it breaks [url] search by replacing & with amp
5
$Text = str_replace("<", "<", $Text);
6
$Text = str_replace(">", ">", $Text);
7
8
9
// Set up the parameters for a URL search string
10
$URLSearchString = " a-zA-Z0-9\:\/\-\?\&\.\=\_\~\#\'";
11
// Set up the parameters for a MAIL search string
12
$MAILSearchString = $URLSearchString . " a-zA-Z0-9\.@";
13
14
//Non BB URL Search
15
//$Text = eregi_replace("([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])", "<a href=\"\\1://\\2\\3\" target=\"_blank\" target=\"_new\">\\1://\\2\\3</a>", $Text);
16
//$Text = eregi_replace("(([a-z0-9_]|\\-|\\.)+@([^[:space:]]*)([[:alnum:]-]))", "<a href=\"mailto:\\1\" target=\"_new\">\\1</a>", $Text);
17
if (substr($Text, 0, 7) == "http://") {
18
$Text = eregi_replace("([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])",
19
"<a href=\"\\1://\\2\\3\">\\1://\\2\\3</a>", $Text);
20
// Convert new line chars to html <br /> tags
21
$Text = nl2br($Text);
22
} else {
23
// Perform URL Search
24
$Text = preg_replace("/\[url\]([$URLSearchString]*)\[\/url\]/",
25
'<a href="javascript:go(\'$1\',\'new\')">$1</a>', $Text);
26
$Text = preg_replace("(\[url\=([$URLSearchString]*)\](.+?)\[/url\])",
27
'<a href="javascript:go(\'$1\',\'new\')">$2</a>', $Text);
28
//$Text = preg_replace("(\[url\=([$URLSearchString]*)\]([$URLSearchString]*)\[/url\])", '<a href="$1" target="_blank">$2</a>', $Text);
29
// Convert new line chars to html <br /> tags
30
$Text = nl2br($Text);
31
}
32
// Perform MAIL Search
33
$Text = preg_replace("(\[mail\]([$MAILSearchString]*)\[/mail\])",
34
'<a href="mailto:$1">$1</a>', $Text);
35
$Text = preg_replace("/\[mail\=([$MAILSearchString]*)\](.+?)\[\/mail\]/",
36
'<a href="mailto:$1">$2</a>', $Text);
37
38
// Check for bold text
39
$Text = preg_replace("(\[b\](.+?)\[\/b])is", '<span class="bold">$1</span>', $Text);
40
41
// Check for Italics text
42
$Text = preg_replace("(\[i\](.+?)\[\/i\])is", '<span class="italics">$1</span>',
43
$Text);
44
45
// Check for Underline text
46
$Text = preg_replace("(\[u\](.+?)\[\/u\])is",
47
'<span class="underline">$1</span>', $Text);
48
49
// Check for strike-through text
50
$Text = preg_replace("(\[s\](.+?)\[\/s\])is",
51
'<span class="strikethrough">$1</span>', $Text);
52
53
// Check for over-line text
54
$Text = preg_replace("(\[o\](.+?)\[\/o\])is", '<span class="overline">$1</span>',
55
$Text);
56
57
// Check for colored text
58
$Text = preg_replace("(\[color=(.+?)\](.+?)\[\/color\])is", "<span style=\"color: $1\">$2</span>",
59
$Text);
60
61
// Check for sized text
62
$Text = preg_replace("(\[size=(.+?)\](.+?)\[\/size\])is", "<span style=\"font-size: $1px\">$2</span>",
63
$Text);
64
65
// Check for list text
66
$Text = preg_replace("/\[list\](.+?)\[\/list\]/is",
67
'<ul class="listbullet">$1</ul>', $Text);
68
$Text = preg_replace("/\[list=1\](.+?)\[\/list\]/is",
69
'<ul class="listdecimal">$1</ul>', $Text);
70
$Text = preg_replace("/\[list=i\](.+?)\[\/list\]/s",
71
'<ul class="listlowerroman">$1</ul>', $Text);
72
$Text = preg_replace("/\[list=I\](.+?)\[\/list\]/s",
73
'<ul class="listupperroman">$1</ul>', $Text);
74
$Text = preg_replace("/\[list=a\](.+?)\[\/list\]/s",
75
'<ul class="listloweralpha">$1</ul>', $Text);
76
$Text = preg_replace("/\[list=A\](.+?)\[\/list\]/s",
77
'<ul class="listupperalpha">$1</ul>', $Text);
78
$Text = str_replace("[*]", "<li>", $Text);
79
80
// Check for font change text
81
$Text = preg_replace("(\[font=(.+?)\](.+?)\[\/font\])", "<span style=\"font-family: $1;\">$2</span>",
82
$Text);
83
84
// Declare the format for [code] layout
85
$CodeLayout = '<table width="90%" border="0" align="center" cellpadding="0" cellspacing="0">
86
<tr>
87
<td class="quotecodeheader"> Code:</td>
88
</tr>
89
<tr>
90
<td class="codebody">$1</td>
91
</tr>
92
</table>';
93
// Check for [code] text
94
$Text = preg_replace("/\[code\](.+?)\[\/code\]/is", "$CodeLayout", $Text);
95
96
// Declare the format for [quote] layout
97
$QuoteLayout = '<table width="90%" border="0" align="center" cellpadding="0" cellspacing="0">
98
<tr>
99
<td class="quotecodeheader"> Quote:</td>
100
</tr>
101
<tr>
102
<td class="quotebody">$1</td>
103
</tr>
104
</table>';
105
106
// Check for [quote] text
107
$Text = preg_replace("/\[quote\](.+?)\[\/quote\]/is", "$QuoteLayout", $Text);
108
109
// Images
110
//
111
$Text = preg_replace("/\[img\](.+?)\[\/img\]/", '<img src="/$1">', $Text);
112
113
// [img=widthxheight]image source[/img]
114
$Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.+?)\[\/img\]/",
115
'<img src="/$3" height="$2" width="$1">', $Text);
116
117
return $Text;
118
}
BBCode CSS
function BBCode($Text) 2
{ 3
// Replace any html brackets with HTML Entities to prevent executing HTML or script 4
// Don't use strip_tags here because it breaks [url] search by replacing & with amp 5
$Text = str_replace("<", "<", $Text); 6
$Text = str_replace(">", ">", $Text); 7
8
9
// Set up the parameters for a URL search string 10
$URLSearchString = " a-zA-Z0-9\:\/\-\?\&\.\=\_\~\#\'"; 11
// Set up the parameters for a MAIL search string 12
$MAILSearchString = $URLSearchString . " a-zA-Z0-9\.@"; 13
14
//Non BB URL Search 15
//$Text = eregi_replace("([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])", "<a href=\"\\1://\\2\\3\" target=\"_blank\" target=\"_new\">\\1://\\2\\3</a>", $Text); 16
//$Text = eregi_replace("(([a-z0-9_]|\\-|\\.)+@([^[:space:]]*)([[:alnum:]-]))", "<a href=\"mailto:\\1\" target=\"_new\">\\1</a>", $Text); 17
if (substr($Text, 0, 7) == "http://") { 18
$Text = eregi_replace("([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])", 19
"<a href=\"\\1://\\2\\3\">\\1://\\2\\3</a>", $Text); 20
// Convert new line chars to html <br /> tags 21
$Text = nl2br($Text); 22
} else { 23
// Perform URL Search 24
$Text = preg_replace("/\[url\]([$URLSearchString]*)\[\/url\]/", 25
'<a href="javascript:go(\'$1\',\'new\')">$1</a>', $Text); 26
$Text = preg_replace("(\[url\=([$URLSearchString]*)\](.+?)\[/url\])", 27
'<a href="javascript:go(\'$1\',\'new\')">$2</a>', $Text); 28
//$Text = preg_replace("(\[url\=([$URLSearchString]*)\]([$URLSearchString]*)\[/url\])", '<a href="$1" target="_blank">$2</a>', $Text); 29
// Convert new line chars to html <br /> tags 30
$Text = nl2br($Text); 31
} 32
// Perform MAIL Search 33
$Text = preg_replace("(\[mail\]([$MAILSearchString]*)\[/mail\])", 34
'<a href="mailto:$1">$1</a>', $Text); 35
$Text = preg_replace("/\[mail\=([$MAILSearchString]*)\](.+?)\[\/mail\]/", 36
'<a href="mailto:$1">$2</a>', $Text); 37
38
// Check for bold text 39
$Text = preg_replace("(\[b\](.+?)\[\/b])is", '<span class="bold">$1</span>', $Text); 40
41
// Check for Italics text 42
$Text = preg_replace("(\[i\](.+?)\[\/i\])is", '<span class="italics">$1</span>', 43
$Text); 44
45
// Check for Underline text 46
$Text = preg_replace("(\[u\](.+?)\[\/u\])is", 47
'<span class="underline">$1</span>', $Text); 48
49
// Check for strike-through text 50
$Text = preg_replace("(\[s\](.+?)\[\/s\])is", 51
'<span class="strikethrough">$1</span>', $Text); 52
53
// Check for over-line text 54
$Text = preg_replace("(\[o\](.+?)\[\/o\])is", '<span class="overline">$1</span>', 55
$Text); 56
57
// Check for colored text 58
$Text = preg_replace("(\[color=(.+?)\](.+?)\[\/color\])is", "<span style=\"color: $1\">$2</span>", 59
$Text); 60
61
// Check for sized text 62
$Text = preg_replace("(\[size=(.+?)\](.+?)\[\/size\])is", "<span style=\"font-size: $1px\">$2</span>", 63
$Text); 64
65
// Check for list text 66
$Text = preg_replace("/\[list\](.+?)\[\/list\]/is", 67
'<ul class="listbullet">$1</ul>', $Text); 68
$Text = preg_replace("/\[list=1\](.+?)\[\/list\]/is", 69
'<ul class="listdecimal">$1</ul>', $Text); 70
$Text = preg_replace("/\[list=i\](.+?)\[\/list\]/s", 71
'<ul class="listlowerroman">$1</ul>', $Text); 72
$Text = preg_replace("/\[list=I\](.+?)\[\/list\]/s", 73
'<ul class="listupperroman">$1</ul>', $Text); 74
$Text = preg_replace("/\[list=a\](.+?)\[\/list\]/s", 75
'<ul class="listloweralpha">$1</ul>', $Text); 76
$Text = preg_replace("/\[list=A\](.+?)\[\/list\]/s", 77
'<ul class="listupperalpha">$1</ul>', $Text); 78
$Text = str_replace("[*]", "<li>", $Text); 79
80
// Check for font change text 81
$Text = preg_replace("(\[font=(.+?)\](.+?)\[\/font\])", "<span style=\"font-family: $1;\">$2</span>", 82
$Text); 83
84
// Declare the format for [code] layout 85
$CodeLayout = '<table width="90%" border="0" align="center" cellpadding="0" cellspacing="0"> 86
<tr> 87
<td class="quotecodeheader"> Code:</td> 88
</tr> 89
<tr> 90
<td class="codebody">$1</td> 91
</tr> 92
</table>'; 93
// Check for [code] text 94
$Text = preg_replace("/\[code\](.+?)\[\/code\]/is", "$CodeLayout", $Text); 95
96
// Declare the format for [quote] layout 97
$QuoteLayout = '<table width="90%" border="0" align="center" cellpadding="0" cellspacing="0"> 98
<tr> 99
<td class="quotecodeheader"> Quote:</td> 100
</tr> 101
<tr> 102
<td class="quotebody">$1</td> 103
</tr> 104
</table>'; 105
106
// Check for [quote] text 107
$Text = preg_replace("/\[quote\](.+?)\[\/quote\]/is", "$QuoteLayout", $Text); 108
109
// Images 110
// 111
$Text = preg_replace("/\[img\](.+?)\[\/img\]/", '<img src="/$1">', $Text); 112
113
// [img=widthxheight]image source[/img] 114
$Text = preg_replace("/\[img\=([0-9]*)x([0-9]*)\](.+?)\[\/img\]/", 115
'<img src="/$3" height="$2" width="$1">', $Text); 116
117
return $Text; 118
} BBCode CSS
1
body {
2
font-family: Verdana, Arial, Helvetica, sans-serif;
3
font-size: 12px;
4
}
5
6
.bold {
7
font-weight: bold;
8
}
9
10
.italics {
11
font-style: italic;
12
}
13
14
.underline {
15
text-decoration: underline;
16
}
17
18
.strikethrough {
19
text-decoration: line-through;
20
}
21
22
.overline {
23
text-decoration: overline;
24
}
25
26
.sized {
27
text-size:
28
}
29
30
.quotecodeheader {
31
font-family: Verdana, arial, helvetica, sans-serif;
32
font-size: 12px;
33
font-weight: bold;
34
}
35
36
.codebody {
37
background-color: #FFFFFF;
38
font-family: Courier new, courier, mono;
39
font-size: 12px;
40
color: #006600;
41
border: 1px solid #BFBFBF;
42
}
43
44
.quotebody {
45
background-color: #FFFFFF;
46
font-family: Courier new, courier, mono;
47
font-size: 12px;
48
color: #660002;
49
border: 1px solid #BFBFBF;
50
}
51
52
.listbullet {
53
list-style-type: disc;
54
list-style-position: inside;
55
}
56
57
.listdecimal {
58
list-style-type: decimal;
59
list-style-position: inside;
60
}
61
62
.listlowerroman {
63
list-style-type: lower-roman;
64
list-style-position: inside;
65
}
66
67
.listupperroman {
68
list-style-type: upper-roman;
69
list-style-position: inside;
70
}
71
72
.listloweralpha {
73
list-style-type: lower-alpha;
74
list-style-position: inside;
75
}
76
77
.listupperalpha {
78
list-style-type: upper-alpha;
79
list-style-position: inside;
80
}
Then just
body {2
font-family: Verdana, Arial, Helvetica, sans-serif;3
font-size: 12px;4
}5

6
.bold {7
font-weight: bold;8
}9

10
.italics {11
font-style: italic;12
}13

14
.underline {15
text-decoration: underline;16
}17

18
.strikethrough {19
text-decoration: line-through;20
}21

22
.overline {23
text-decoration: overline;24
}25

26
.sized {27
text-size:28
}29

30
.quotecodeheader {31
font-family: Verdana, arial, helvetica, sans-serif;32
font-size: 12px;33
font-weight: bold;34
}35

36
.codebody {37
background-color: #FFFFFF;38
font-family: Courier new, courier, mono;39
font-size: 12px;40
color: #006600;41
border: 1px solid #BFBFBF;42
}43

44
.quotebody {45
background-color: #FFFFFF;46
font-family: Courier new, courier, mono;47
font-size: 12px;48
color: #660002;49
border: 1px solid #BFBFBF;50
}51

52
.listbullet {53
list-style-type: disc;54
list-style-position: inside;55
}56

57
.listdecimal {58
list-style-type: decimal;59
list-style-position: inside;60
}61

62
.listlowerroman {63
list-style-type: lower-roman;64
list-style-position: inside;65
}66

67
.listupperroman {68
list-style-type: upper-roman;69
list-style-position: inside;70
}71

72
.listloweralpha {73
list-style-type: lower-alpha;74
list-style-position: inside;75
}76

77
.listupperalpha {78
list-style-type: upper-alpha;79
list-style-position: inside;80
}PHP Code:
1
echo BBCode($string_to_convert);
echo BBCode($string_to_convert);

}
浙公网安备 33010602011771号