UBB parse script zonder gedoe
Een uitgebreide UBB parser die heel makkelijk in gebruik is: Je gebruikt 'm zo: Het is verstandig om dit inclusief met een database te gebruiken aangezien je geen PHP code in een string krijgt enzovoort ;) Voorbeeld: Zo gaat ie erin: http://vuvan.vu.funpic.org/code.txt Zo gaat ie eruit: http://vuvan.vu.funpic.org/high.php Opbouwende kritiek is wederom zeer welkom! Negatieve reacties laat ik links liggen ;) Ideeën die ik hier nog meer in kan zetten zie ik graag tegemoet! Smilies zijn voor de beginnende gebruiker ook makkelijk toe te voegen. Helaas ontstaat er een probleem als je in je PHP script te lange regels hebt, dan gaat er iets fout met de regelnummers, maarja daar wist niemand een antwoord op in het forum :/ Alle functies tot nu toe: HTML verbieden PHP higlight met regelnummers (!) Enters omzetten naar Quotes (ook quote in quote) & Automatische links en emails > klikbaar Links & bla Email & bla Cursieve tekst Vetgedrukte tekst Onderstreepte tekst Doorgestreepte tekst Afbeeldingen Gekleurde tekst Tekstgrootte Blinkende tekst Bewegende tekst & Links uitlijnen Rechts uitlijnen Centreren Superscript Subscript Allcaps Smallcaps Grote tekst Kleine tekst Youtube filmpjes Typemachine tekst Smilies :)
[code]<style type="text/css">
.num {
float: left;
color: gray;
font-size: 13px;
text-align: right;
margin-right: 6pt;
padding-right: 6pt;
border-right: 1px solid gray;}
</style>
<?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);
$aLines = explode("<br />", $code);
$iLines = count($aLines);
$output = "<br /><div style=\"width: 116%;\"><b>PHP code</b><br /><div class=\"num\">";
for($i = 1; $i <= $iLines; $i++){
$output .= $i ."<br />";
}
$output .= "</div><div style=\"float: left;overflow-x: scroll;overflow: auto;display: block;width:80%;\">\n$code\n</div><div style=\"clear:left;\"></div></div>";
return $output;
}
?> [/code]
Reacties
0