[PHP]UBB
Ik ben bezig met een simpele ubb parser..
Nu ben ik bezig met het [ *code* ] [ */code* ] gedeelte..
Als eerste encode ik alles wat tussen de [ *code* ] [ */code* ] tags staat zodat het niet geparsed word..
Daarna decode ik het maar dan staat er niks meer tussen..
En ik heb ook niet echt een idee hoe ik een goede codehighlighter maak met regelnummers.... Ik heb al veel scripts van anderen gezien maar die verschillen veel van elkaar...
Klik hier voor de code
Kan iemand me hiermee helpen?
Alvast bedankt
Alles natuurlijk zonder die *
Gewijzigd op 01/01/1970 01:00:00 door Gebruiker PHP
Zelf gebruik ik geshi voor higlighting...
Ik zoek iets simpels.. Zoals de meeste ubb parsers hebben.
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
90
91
92
93
94
95
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
<?php
//function.php
//ubb phraser
function showsource($text){
$text = str_replace("<br />","",$text);
$text = str_replace("<","<",$text);
$text = str_replace(">",">",$text);
ob_start();
highlight_string(stripslashes($text));
$source = ob_get_contents();
ob_end_clean();
$source = explode('<br />',$source);
$geheel .= '</p><div id="code"><div id="num"><code id="select">';
for ($i = 1; $i < count($source)+1; $i++) {
$geheel .= $i.'.<br />';
}
$geheel .= '</code></div><code>';
for ($i = 0; $i < count($source); $i++) {
$src .= $source[$i].'<br />';
}
$src .= $source[count($source)];
$geheel .= $src.'</code></div><p>';
$geheel = str_replace('&','&',$geheel);
$geheel = str_replace('<font color="','<span style="color:',$geheel);
$geheel = str_replace("</font>","</span>",$geheel);
return $geheel;
}
function ubb($text)
{
global $basedir;
//replace html/php
$text = htmlentities($text);
$text = preg_replace('/\[quote\](.*?)\[\/quote\]/si','<div class="quote">\\1</div>',$text);
$text = nl2br($text);
$smiley = array(':)',';)',':D','8)',':p',':rolleyes:',':S',':@',':(',':O',':|',':cry:');
$image = array('smile','wink','laughing','cool','tongue','rolleyes','confused','angry','sad','surprised','wasset','crying');
for($s=0;$s < 12; $s++)
{
$smil = "<img src=\"".$basedir."images/smiles/".$image[$s].".gif\" alt=\"(".$image[$s].")\" />";
$text = str_replace($smiley[$s],$smil, $text);
}
//layout
$text = preg_replace('/\[b\](.*?)\[\/b\]/si','<strong>\\1</strong>',$text);
$text = preg_replace('/\[i\](.*?)\[\/i\]/si','<em>\\1</em>',$text);
$text = preg_replace('/\[u\](.*?)\[\/u\]/si','<span class="underline">\\1</span>',$text);
//images
$text = preg_replace("#\[img\](http(s)?://)([a-zA-Z0-9\-\.,\?!%\*_\#:;~\\&$@\/=\+]+)\[/img\]#", "<img src='\\1\\3' alt='\\1\\3'/>", $text);
$images = array();
$replace = array();
$images[0] = '#\[img height=([0-9]*?) width=([0-9]*?)\](.*?)\[\/img\]#si';
$images[1] = '#\[img width=([0-9]*?) height=([0-9]*?)\](.*?)\[\/img\]#si';
$images[2] = '#\[imgl\](.*?)\[\/imgl\]#si';
$images[3] = '#\[imgr\](.*?)\[\/imgr\]#si';
$replace[0] = '<img src="\\3" height="\\1" width="\\2" alt="\\3"/>';
$replace[1] = '<img src="\\3" height="\\2" width="\\1" alt="\\3"/>';
$replace[2] = '<img scr="\\1" class="imageleft" alt="\\1"/>';
$replace[3] = '<img scr="\\1" class="imageright" alt="\\1"/>';
$text = preg_replace($images,$replace,$text);
//links
$linktype = array();
$replacement = array();
$linktype[0] = '#\[url\]([a-z]*?://){1}(.*?)\[\/url\]#si';
$replacement[0] = '<a href="\\1\\2" rel="external">\\1\\2</a>';
$linktype[1] = '#\[url\](.*?)\[\/url\]#si';
$replacement[1] = '<a href="http://\\1" rel="external">\\1</a>';
$linktype[2] = '#\[url=([a-z]*?://){1}(.*?)\](.*?)\[\/url\]#si';
$replacement[2] = '<a href="\\1\\2" rel="external">\\3</a>';
$linktype[3] = '#\[url=(.*?)\](.*?)\[\/url\]#si';
$replacement[3] = '<a href="http://\\1" rel="external">\\2</a>';
$linktype[4] = '#\[email\](.*?)\[\/email\]#si';
$replacement[4] = '<a href="mailto:\\1">\\1</a>';
$linktype[5] = '#\[email=(.*?)\](.*?)\[\/email\]#si';
$replacement[5] = '<a href="mailto:\\1">\\2</a>';
$text = preg_replace($linktype,$replacement,$text);
//other nice options:
$text = preg_replace('/\[warning\](.*?)\[\/warning\]/si','</p><div class="warning">\\1</div><p>',$text);
$text = preg_replace('/\[code\](.*?)\[\/code\]/esiU', "showsource('$1')",$text);
$text = preg_replace('/\[quote\](.*?)\[\/quote\]/si','</p><div class="quote">\\1</div><p>',$text);
$text = preg_replace('/\[quote=([0-9]*?),(.*?)\](.*?)\[\/quote\]/si','</p><div class="quote"><a href="#reactie\\1">\\2</a><br />\\3</div><p>',$text);
//Om als voorbeeld BB weer tegen moeten [ BB ] om gezet worden naar [BB]
$text = preg_replace('/\[ (.*?) \](.*?)\[ \/(.*?) \]/si','[\\1]\\2[/\\3]',$text);
return $text;
}
?>
//function.php
//ubb phraser
function showsource($text){
$text = str_replace("<br />","",$text);
$text = str_replace("<","<",$text);
$text = str_replace(">",">",$text);
ob_start();
highlight_string(stripslashes($text));
$source = ob_get_contents();
ob_end_clean();
$source = explode('<br />',$source);
$geheel .= '</p><div id="code"><div id="num"><code id="select">';
for ($i = 1; $i < count($source)+1; $i++) {
$geheel .= $i.'.<br />';
}
$geheel .= '</code></div><code>';
for ($i = 0; $i < count($source); $i++) {
$src .= $source[$i].'<br />';
}
$src .= $source[count($source)];
$geheel .= $src.'</code></div><p>';
$geheel = str_replace('&','&',$geheel);
$geheel = str_replace('<font color="','<span style="color:',$geheel);
$geheel = str_replace("</font>","</span>",$geheel);
return $geheel;
}
function ubb($text)
{
global $basedir;
//replace html/php
$text = htmlentities($text);
$text = preg_replace('/\[quote\](.*?)\[\/quote\]/si','<div class="quote">\\1</div>',$text);
$text = nl2br($text);
$smiley = array(':)',';)',':D','8)',':p',':rolleyes:',':S',':@',':(',':O',':|',':cry:');
$image = array('smile','wink','laughing','cool','tongue','rolleyes','confused','angry','sad','surprised','wasset','crying');
for($s=0;$s < 12; $s++)
{
$smil = "<img src=\"".$basedir."images/smiles/".$image[$s].".gif\" alt=\"(".$image[$s].")\" />";
$text = str_replace($smiley[$s],$smil, $text);
}
//layout
$text = preg_replace('/\[b\](.*?)\[\/b\]/si','<strong>\\1</strong>',$text);
$text = preg_replace('/\[i\](.*?)\[\/i\]/si','<em>\\1</em>',$text);
$text = preg_replace('/\[u\](.*?)\[\/u\]/si','<span class="underline">\\1</span>',$text);
//images
$text = preg_replace("#\[img\](http(s)?://)([a-zA-Z0-9\-\.,\?!%\*_\#:;~\\&$@\/=\+]+)\[/img\]#", "<img src='\\1\\3' alt='\\1\\3'/>", $text);
$images = array();
$replace = array();
$images[0] = '#\[img height=([0-9]*?) width=([0-9]*?)\](.*?)\[\/img\]#si';
$images[1] = '#\[img width=([0-9]*?) height=([0-9]*?)\](.*?)\[\/img\]#si';
$images[2] = '#\[imgl\](.*?)\[\/imgl\]#si';
$images[3] = '#\[imgr\](.*?)\[\/imgr\]#si';
$replace[0] = '<img src="\\3" height="\\1" width="\\2" alt="\\3"/>';
$replace[1] = '<img src="\\3" height="\\2" width="\\1" alt="\\3"/>';
$replace[2] = '<img scr="\\1" class="imageleft" alt="\\1"/>';
$replace[3] = '<img scr="\\1" class="imageright" alt="\\1"/>';
$text = preg_replace($images,$replace,$text);
//links
$linktype = array();
$replacement = array();
$linktype[0] = '#\[url\]([a-z]*?://){1}(.*?)\[\/url\]#si';
$replacement[0] = '<a href="\\1\\2" rel="external">\\1\\2</a>';
$linktype[1] = '#\[url\](.*?)\[\/url\]#si';
$replacement[1] = '<a href="http://\\1" rel="external">\\1</a>';
$linktype[2] = '#\[url=([a-z]*?://){1}(.*?)\](.*?)\[\/url\]#si';
$replacement[2] = '<a href="\\1\\2" rel="external">\\3</a>';
$linktype[3] = '#\[url=(.*?)\](.*?)\[\/url\]#si';
$replacement[3] = '<a href="http://\\1" rel="external">\\2</a>';
$linktype[4] = '#\[email\](.*?)\[\/email\]#si';
$replacement[4] = '<a href="mailto:\\1">\\1</a>';
$linktype[5] = '#\[email=(.*?)\](.*?)\[\/email\]#si';
$replacement[5] = '<a href="mailto:\\1">\\2</a>';
$text = preg_replace($linktype,$replacement,$text);
//other nice options:
$text = preg_replace('/\[warning\](.*?)\[\/warning\]/si','</p><div class="warning">\\1</div><p>',$text);
$text = preg_replace('/\[code\](.*?)\[\/code\]/esiU', "showsource('$1')",$text);
$text = preg_replace('/\[quote\](.*?)\[\/quote\]/si','</p><div class="quote">\\1</div><p>',$text);
$text = preg_replace('/\[quote=([0-9]*?),(.*?)\](.*?)\[\/quote\]/si','</p><div class="quote"><a href="#reactie\\1">\\2</a><br />\\3</div><p>',$text);
//Om als voorbeeld BB weer tegen moeten [ BB ] om gezet worden naar [BB]
$text = preg_replace('/\[ (.*?) \](.*?)\[ \/(.*?) \]/si','[\\1]\\2[/\\3]',$text);
return $text;
}
?>
Thanks.. Morge ga ik dat zeker goed doorspitten:-P
Code (php)
1
2
3
2
3
<?php
$content = preg_replace('#\[code\](.*?)\[\/code\]#se',"highlight_string(html_entity_decode('$1'),true)",$content);
?>
$content = preg_replace('#\[code\](.*?)\[\/code\]#se',"highlight_string(html_entity_decode('$1'),true)",$content);
?>
Er zijn mensen die weten dat ik er redelijk lang aan gepuzzelt heb wou het kloppen :)
Code (php)
1
2
3
2
3
<?php
$content = preg_replace('#\[code\](.+?)\[/code\]#sie',"highlight_string(html_entity_decode('\\1'),true)",$content);
?>
$content = preg_replace('#\[code\](.+?)\[/code\]#sie',"highlight_string(html_entity_decode('\\1'),true)",$content);
?>
Eigenlijk moet je er ook nog een stripslashes overheen halen, de 'e' modifier doet er automatich een addslashes overheen voordat ie um evalueerd als php.
Gewijzigd op 01/01/1970 01:00:00 door The Beeding Clown
Ik heb er alleen htmlentities() nog voor staan, en toch werkt hij goed.
(de stripslashes moet ik wel over de string halen die mee genomen word door de preg_replace, niet van te voren.. view the test page source)
// "e" modifier Met de stripslashes
http://www.niele.nl/_playground/phpubb.php?stripslashes=true
// "e" modifier Zonder de stripslashes
http://www.niele.nl/_playground/phpubb.php?stripslashes=false
// source van de test page
http://www.niele.nl/_playground/phpubb.phps
Gewijzigd op 01/01/1970 01:00:00 door The Beeding Clown