UBB code
Deze werkt nu bijna. Alleen ik heb een probleem bij het volgende.
Als ik een stuk tekst type dan pakt hij de kleur goed op die ik standaard gebruik.
Maar als ik bijvoorbeeld
Code (php)
1
2
3
4
5
6
7
2
3
4
5
6
7
Deze tekst is grijs (standaard)
<?php
echo 'Staat tussen code tags.';
?>
Deze tekst is zwart(hoe kan dit.)
<?php
echo 'Staat tussen code tags.';
?>
Deze tekst is zwart(hoe kan dit.)
Website is hier.
Op de site kun je zien wat ik precies bedoel.
Code (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
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
<?php
## Alle UBB-tags converteren
function makeUBB($string){
// HTML verbieden
$string = htmlspecialchars($string);
// Enters maken
$string = nl2br($string);
// Links maken
$string = preg_replace("_\[url]http://(.*)\[/url\]_si", '<a href="$1" target="_blank">$1</a>', $string);
$string = preg_replace("_\[url](.*)\[/url\]_si", '<a href="http://$1" target="_blank">$1</a>', $string);
$string = preg_replace("_\[url=http://(.*)\](.*?)\[/url\]_si", '<a href="$1" target="_blank">$2</a>', $string);
$string = preg_replace("_\[url=(.*)\](.*?)\[/url\]_si", '<a href="http://$1" target="_blank">$2</a>', $string);
$string = preg_replace("_\[email](.*)\[/email\]_si", '<a href="mailto://$1">$1</a>', $string);
$string = preg_replace("_\[email=(.*)\](.*?)\[/email\]_si", '<a href="mailto:$1">$2</a>', $string);
// Afbeeldingen
$string = preg_replace("_\[img](.*)\[/img\]_si", '<img src="$1" alt="Afbeelding" />', $string);
// Automatisch links maken
$string = preg_replace('#(^|[ \n\r\t])([a-z0-9]{1,6}://([a-z0-9\-]{1,}(\.?)){1,}[a-z]{2,5}(:[0-9]{2,5}){0,1}((\/|~|\#|\?|=|&|&|\+){1}[a-z0-9\-._%]{0,}){0,})#si', '\\1<a href="\\2">\\2</a>', $string);
$string = preg_replace('#(^|[ \n\r\t])((www\.){1}([a-z0-9\-]{1,}(\.?)){1,}[a-z]{2,5}(:[0-9]{2,5}){0,1}((\/|~|\#|\?|=|&|&|\+){1}[a-z0-9\-._%]{0,}){0,})#si', '\\1<a href="http://\\2">\\2</a>', $string);
$string = preg_replace('#(^|[ \n\r\t])(([a-z0-9\-_]{1,}(\.?)){1,}@([a-z0-9\-]{1,}(\.?)){1,}[a-z]{2,5})#si', '\\1<a href="mailto:\\2">\\2</a>', $string);
// Youtube video
$string = preg_replace('_\[youtube\].*?(v=|v/)(.+?)(&.*?|/.*?)?\[/youtube\]_is', '[youtube]$2[/youtube]', $string);
$string = preg_replace('_\[youtube\]([a-z0-9-]+?)\[/youtube\]_is', '<object width="425" height="355"><param name="movie" value="http://www.youtube.com/v/$1"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/$1" type="application/x-shockwave-flash" wmode="transparent" width="425" height="355"></embed></object>', $string);
// Uitlijnen
$string = preg_replace("_\[left](.*)\[/left\]_si", '<div align="left">$1</div>', $string);
$string = preg_replace("_\[right](.*)\[/right\]_si", '<div align="right" style="margin-right: 5px;">$1</div>', $string);
$string = preg_replace("_\[center](.*)\[/center\]_si", '<div align="center">$1</div>', $string);
// Kleuren
$string = preg_replace("_\[color=(.*)\](.*?)\[/color\]_si", '<span style="color: $1">$2</span>', $string);
// Tekstgrootte
$string = preg_replace("_\[size=(.*)\](.*?)\[/size\]_si", '<span style="font-size: $1">$2</span>', $string);
$string = preg_replace("_\[tt](.*)\[/tt\]_si", '<tt>$1</tt>', $string);
$string = preg_replace("_\[big](.*)\[/big\]_si", '<big>$1</big>', $string);
$string = preg_replace("_\[small](.*)\[/small\]_si", '<small>$1</small>', $string);
// Vetgedrukt
$string = preg_replace("_\[b\](.*?)\[/b\]_si", '<b>$1</b>', $string);
// Cursief
$string = preg_replace("_\[i\](.*?)\[/i\]_si", '<i>$1</i>', $string);
// Onderstrepen
$string = preg_replace("_\[u\](.*?)\[/u\]_si", '<u>$1</u>', $string);
// Doorstrepen
$string = preg_replace("_\[s\](.*?)\[/s\]_si", '<s>$1</s>', $string);
// Knipperen
$string = preg_replace("_\[blink\](.*?)\[/blink\]_si", '<blink>$1</blink>', $string);
// Superscript
$string = preg_replace("_\[sup\](.*?)\[/sup\]_si", '<sup>$1</sup>', $string);
// Subscript
$string = preg_replace("_\[sub\](.*?)\[/sub\]_si", '<sub>$1</sub>', $string);
// All caps
$string = preg_replace("_\[ac\](.*?)\[/ac\]_si", '<span style="text-transform:uppercase">$1</span>', $string);
// Small caps
$string = preg_replace("_\[sc\](.*?)\[/sc\]_si", '<span style="text-transform:lowercase">$1</span>', $string);
// Marquee, standaard "scroll", andere opties: "slide" & "alternate"
$string = preg_replace("_\[slide\](.*?)\[/slide\]_si", '<marquee>$1</marquee>', $string);
$string = preg_replace("_\[slide=(.*?)\](.*?)\[/slide\]_si", '<marquee behavior="$1">$2</marquee>', $string);
// Smilies
$string = str_replace(":)"," <img src=\"images/smilies/lach.gif\" alt=\"lach\" />", $string);
// Quotes
while(preg_match("((\[quote=(.+?)\](.+?)\[\/quote\])|(\[quote\](.+?)\[\/quote]))is", $string)) {
$string = preg_replace("(\[quote=(.+?)\](.+?)\[\/quote\])is",'<fieldset><legend> <b>$1 schreef:</b> </legend><br />$2<br /></fieldset><br />' ,$string);
$string = preg_replace("(\[quote\](.+?)\[\/quote])is",'<fieldset><legend> <b>Quote</b> </legend><br />$1<br /></fieldset><br />' ,$string);
}
// Highlighten
$string = preg_replace_callback("_\[code\](.*?)\[/code\]_is", "customHighlight", $string);
return $string;
}
function customHighlight($code){
$code = highlight_string(htmlspecialchars_decode(strip_tags($code[1])), true);
$code = str_replace('\"', '"', $code);
$code = str_replace("?>", "<span style=\"color: #0000BB\">?></span>", $code);//#0000BB
$aLines = explode("<br />", $code);
$iLines = count($aLines);
$output = "<br /><div style=\"width: 116%;\"><b><font color='#fff'>Code:</font></b><br /><div class=\"num\">";
for($i = 1; $i <= $iLines; $i++){
$output .= $i ."<br />";
}
$output .= "</div><div style=\"float: left;overflow-x: auto;display: block;width:70%; background:#d3d3d3;;\">\n$code\n</div><div style=\"clear:left;\"></div></div>";
return $output;
}
?>
## Alle UBB-tags converteren
function makeUBB($string){
// HTML verbieden
$string = htmlspecialchars($string);
// Enters maken
$string = nl2br($string);
// Links maken
$string = preg_replace("_\[url]http://(.*)\[/url\]_si", '<a href="$1" target="_blank">$1</a>', $string);
$string = preg_replace("_\[url](.*)\[/url\]_si", '<a href="http://$1" target="_blank">$1</a>', $string);
$string = preg_replace("_\[url=http://(.*)\](.*?)\[/url\]_si", '<a href="$1" target="_blank">$2</a>', $string);
$string = preg_replace("_\[url=(.*)\](.*?)\[/url\]_si", '<a href="http://$1" target="_blank">$2</a>', $string);
$string = preg_replace("_\[email](.*)\[/email\]_si", '<a href="mailto://$1">$1</a>', $string);
$string = preg_replace("_\[email=(.*)\](.*?)\[/email\]_si", '<a href="mailto:$1">$2</a>', $string);
// Afbeeldingen
$string = preg_replace("_\[img](.*)\[/img\]_si", '<img src="$1" alt="Afbeelding" />', $string);
// Automatisch links maken
$string = preg_replace('#(^|[ \n\r\t])([a-z0-9]{1,6}://([a-z0-9\-]{1,}(\.?)){1,}[a-z]{2,5}(:[0-9]{2,5}){0,1}((\/|~|\#|\?|=|&|&|\+){1}[a-z0-9\-._%]{0,}){0,})#si', '\\1<a href="\\2">\\2</a>', $string);
$string = preg_replace('#(^|[ \n\r\t])((www\.){1}([a-z0-9\-]{1,}(\.?)){1,}[a-z]{2,5}(:[0-9]{2,5}){0,1}((\/|~|\#|\?|=|&|&|\+){1}[a-z0-9\-._%]{0,}){0,})#si', '\\1<a href="http://\\2">\\2</a>', $string);
$string = preg_replace('#(^|[ \n\r\t])(([a-z0-9\-_]{1,}(\.?)){1,}@([a-z0-9\-]{1,}(\.?)){1,}[a-z]{2,5})#si', '\\1<a href="mailto:\\2">\\2</a>', $string);
// Youtube video
$string = preg_replace('_\[youtube\].*?(v=|v/)(.+?)(&.*?|/.*?)?\[/youtube\]_is', '[youtube]$2[/youtube]', $string);
$string = preg_replace('_\[youtube\]([a-z0-9-]+?)\[/youtube\]_is', '<object width="425" height="355"><param name="movie" value="http://www.youtube.com/v/$1"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/$1" type="application/x-shockwave-flash" wmode="transparent" width="425" height="355"></embed></object>', $string);
// Uitlijnen
$string = preg_replace("_\[left](.*)\[/left\]_si", '<div align="left">$1</div>', $string);
$string = preg_replace("_\[right](.*)\[/right\]_si", '<div align="right" style="margin-right: 5px;">$1</div>', $string);
$string = preg_replace("_\[center](.*)\[/center\]_si", '<div align="center">$1</div>', $string);
// Kleuren
$string = preg_replace("_\[color=(.*)\](.*?)\[/color\]_si", '<span style="color: $1">$2</span>', $string);
// Tekstgrootte
$string = preg_replace("_\[size=(.*)\](.*?)\[/size\]_si", '<span style="font-size: $1">$2</span>', $string);
$string = preg_replace("_\[tt](.*)\[/tt\]_si", '<tt>$1</tt>', $string);
$string = preg_replace("_\[big](.*)\[/big\]_si", '<big>$1</big>', $string);
$string = preg_replace("_\[small](.*)\[/small\]_si", '<small>$1</small>', $string);
// Vetgedrukt
$string = preg_replace("_\[b\](.*?)\[/b\]_si", '<b>$1</b>', $string);
// Cursief
$string = preg_replace("_\[i\](.*?)\[/i\]_si", '<i>$1</i>', $string);
// Onderstrepen
$string = preg_replace("_\[u\](.*?)\[/u\]_si", '<u>$1</u>', $string);
// Doorstrepen
$string = preg_replace("_\[s\](.*?)\[/s\]_si", '<s>$1</s>', $string);
// Knipperen
$string = preg_replace("_\[blink\](.*?)\[/blink\]_si", '<blink>$1</blink>', $string);
// Superscript
$string = preg_replace("_\[sup\](.*?)\[/sup\]_si", '<sup>$1</sup>', $string);
// Subscript
$string = preg_replace("_\[sub\](.*?)\[/sub\]_si", '<sub>$1</sub>', $string);
// All caps
$string = preg_replace("_\[ac\](.*?)\[/ac\]_si", '<span style="text-transform:uppercase">$1</span>', $string);
// Small caps
$string = preg_replace("_\[sc\](.*?)\[/sc\]_si", '<span style="text-transform:lowercase">$1</span>', $string);
// Marquee, standaard "scroll", andere opties: "slide" & "alternate"
$string = preg_replace("_\[slide\](.*?)\[/slide\]_si", '<marquee>$1</marquee>', $string);
$string = preg_replace("_\[slide=(.*?)\](.*?)\[/slide\]_si", '<marquee behavior="$1">$2</marquee>', $string);
// Smilies
$string = str_replace(":)"," <img src=\"images/smilies/lach.gif\" alt=\"lach\" />", $string);
// Quotes
while(preg_match("((\[quote=(.+?)\](.+?)\[\/quote\])|(\[quote\](.+?)\[\/quote]))is", $string)) {
$string = preg_replace("(\[quote=(.+?)\](.+?)\[\/quote\])is",'<fieldset><legend> <b>$1 schreef:</b> </legend><br />$2<br /></fieldset><br />' ,$string);
$string = preg_replace("(\[quote\](.+?)\[\/quote])is",'<fieldset><legend> <b>Quote</b> </legend><br />$1<br /></fieldset><br />' ,$string);
}
// Highlighten
$string = preg_replace_callback("_\[code\](.*?)\[/code\]_is", "customHighlight", $string);
return $string;
}
function customHighlight($code){
$code = highlight_string(htmlspecialchars_decode(strip_tags($code[1])), true);
$code = str_replace('\"', '"', $code);
$code = str_replace("?>", "<span style=\"color: #0000BB\">?></span>", $code);//#0000BB
$aLines = explode("<br />", $code);
$iLines = count($aLines);
$output = "<br /><div style=\"width: 116%;\"><b><font color='#fff'>Code:</font></b><br /><div class=\"num\">";
for($i = 1; $i <= $iLines; $i++){
$output .= $i ."<br />";
}
$output .= "</div><div style=\"float: left;overflow-x: auto;display: block;width:70%; background:#d3d3d3;;\">\n$code\n</div><div style=\"clear:left;\"></div></div>";
return $output;
}
?>
Gewijzigd op 24/01/2012 19:45:44 door Bas van de Steeg
Omdat je code blok het paragraaf element < p > onderbreekt. Daarmee zal een browser het afsluiten. Een volgend stuk normale tekst staat dus niet meer in paragraaf tags en krijgt dus ook niet de styling.
Edit:
Het is me nu gelukt, bedankt Erwin het was een steuntje in de goede richting met zoeken.
Gewijzigd op 24/01/2012 19:53:13 door Bas van de Steeg