owls
view bbcodeparser.inc.php @ 18:98983ba58e2e
Added tag owls-2.0 for changeset 081ba8764994
author | markus schnalke <meillo@marmaro.de> |
---|---|
date | Sat, 23 Jul 2016 21:39:40 +0200 |
parents | 3e3fa7725abb |
children |
line source
1 <?php
2 /**
3 * BB-Code-Parser
4 *
5 * @author Meillo r e t u r n s <meillo@marmaro.de>
6 */
9 /// path to smilies
10 define('SMILIE_DIR', 'smilies/');
14 /**
15 * parses recursive quotes without a source mentioned
16 *
17 * @param $textinput the bbcode-text
18 * @param $level number of levels to go inside
19 * @return HTML-text
20 */
21 function parse_quote1($textinput,$level = 1) {
22 $pattern = '#\[quote\](((?R)|(.*))*)\[/quote\]#isUe';
23 $replacement = "'<br />[nl]<span class=\"quote0\">Zitat:</span>[nl]<div class=\"quote1\">[nl]'.parse_quote1('$1',
24 ". ($level + 1) ."
25 ).'[nl]</div>[nl]'";
26 return preg_replace($pattern, $replacement, $textinput);
27 }
29 /**
30 * parses recursive quotes with a source mentioned
31 *
32 * @param $textinput the bbcode-text
33 * @param $level number of levels to go inside
34 * @return HTML-text
35 */
36 function parse_quote2($textinput,$level = 1) {
37 $pattern = '#\[quote\=(.*)\](((?R)|(.*))*)\[/quote\]#isUe';
38 $replacement = "'<br />[nl]<span class=\"quote0\">Zitat: ($1)</span>[nl]<div class=\"quote2\">[nl]'.parse_quote2('$2',
39 ". ($level + 1) ."
40 ).'[nl]</div>[nl]'";
41 return preg_replace($pattern, $replacement, $textinput);
42 }
44 /**
45 * replaces smilies
46 *
47 * @param $text text with ASCII-smilies
48 * @return text with [img]-smilies
49 */
50 function smilies($text) {
51 $smilies = array( ':-?\)' => 'Smile.gif',
52 ';-?\)' => 'Wink.gif',
53 ':-?D' => 'Biggrin.gif',
54 ':-?\(' => 'Sad.gif',
55 ':-?P' => 'Lick.gif',
56 ':o' => 'Talk.gif',
57 ':-S' => 'Dontknow.gif',
58 ':dontknow:' => 'Dontknow.gif',
59 ':-@' => 'Angry.gif',
60 ':cool:' => 'Cool.gif',
61 'B-\)' => 'Cool.gif',
62 '%-\)' => 'Crosseyed.gif',
63 '%-\(' => 'Crosseyed.gif',
64 ':rolleyes:' => 'Rolleyes.gif',
65 ':eek:' => 'Shocked.gif');
66 while(list($key, $val) = each($smilies)) {
67 $key = '/(\s|^)'.$key.'(\s|$)/';
68 $text = preg_replace($key, '$1[img]'. SMILIE_DIR . $val .'[/img]$2', $text);
69 }
70 return $text;
71 }
74 /**
75 * turns bbcode in HTML
76 *
77 * @param $text the text with bbcode inside
78 * @param $smilies set to 1 causes smilies to be replaced with pics
79 * @param $images set to 1 causes images to be displayed ([img]-tag)
80 * @return text with HTML-code
81 */
82 function bbcode($text, $smilies = 0, $images = 0) {
84 // line breaks and special chars
85 $text = preg_replace("#(\r\n)|(\r)#", "\n", htmlentities($text, ENT_COMPAT, 'UTF-8'));
86 $text = preg_replace("#(\n){3,}#", "\n\n", $text);
88 // smilies
89 if ($smilies == 1) {
90 $text = smilies($text);
91 }
93 /*
94 // new-lines
95 $text = preg_replace("#(\r\n)|(\r)#", "\n", $text);
96 $text = str_replace("\n", '<br />[nl]', htmlentities($text, ENT_COMPAT, 'UTF-8'));
98 // bold
99 $text = preg_replace("#\[b\](.*?)\[/b\]#i", "<strong>$1</strong>", $text);
100 // italic
101 $text = preg_replace("#\[i\](.*?)\[/i\]#i", "<i>$1</i>", $text);
102 // links
103 $text = preg_replace("#\[url\](.*)\[/url\]#iU", "<a href=\"$1\">$1</a>", $text);
104 $text = preg_replace("#\[url=(.*)\](.*)\[/url\]#iU", "<a href=\"$1\">$2</a>", $text);
105 // lists
106 //$text = preg_replace("#\[list\]\<br /\>(.*)\[/list\]#iU", "<ul>$1</ul>", $text);
107 //$text = preg_replace("#\[\*\](.*)\<br \/\>#iU", "<li>$1</li>", $text);
108 // quotes
109 */
110 $text = parse_quote1($text);
111 $text = parse_quote2($text);
112 // images
113 if ($images == 1) {
114 $text = preg_replace("#\[img\](.*?)\[/img\]#i", "<img src=\"$1\" alt=\"<[Bild]>\" />", $text);
115 }
117 /*
118 // remove backslashes
119 $text = preg_replace("#\\\#is", "", $text);
120 // new-lines
121 $text = str_replace('[nl]', "\n", $text);
122 */
125 // inline
126 $text = preg_replace('#\[b\](.*)\[/b\]#iU', '<strong>$1</strong>', $text);
127 $text = preg_replace('#\*(.*)\*#iU', '<strong>$1</strong>', $text);
128 $text = preg_replace('#\[i\](.*)\[/i\]#iU', '<i>$1</i>', $text);
129 $text = preg_replace('#[^A-Za-z0-9]_(.*)_[^A-Za-z0-9]#iU', '<em>$1</em>', $text);
130 $text = preg_replace('#{{{(.*)}}}#iUs', '<tt>$1</tt>', $text);
131 $text = preg_replace("#\[url\](.*)\[/url\]#iU","<a href=\"$1\">$1</a>",$text);
132 $text = preg_replace("#\[url=(.*)\](.*)\[/url\]#iU","<a href=\"$1\">$2</a>",$text);
133 $text = preg_replace("#\[img\](.*)\[/img\]#iU","<img src=\"$1\" alt=\"[Bild]\" />", $text);
134 $text = preg_replace("#^-{3,}$#iUm","<hr />", $text);
136 // Listen
137 # $text = preg_replace("#\[list\](.*)\[/list\]#i","<ul>$1</ul>", $text);
138 $text = preg_replace("#^\s*\*(.*)\n#imU", "<li>$1</li>", $text);
139 $text = preg_replace("#^((\s*\<li\>(.*)\</li\>\s*)+)$#im", "<ul>$1</ul>", $text);
144 // boxes
147 $text = preg_replace('#^(.+)$#mU', "<p>\n $1\n</p>", $text);
149 # nicht wenn <h_> oder <ul> enthalten sind
150 $text = preg_replace("#\<p\>\n \[h([1-6]{1})\](.*)\[/h[1-6]{1}\]\n\</p\>#iU", "\n<h$1>$2</h$1>", $text);
151 $text = preg_replace("#\<p\>\n ===(.*)\n\</p\>#iU", "\n<h3>$1</h3>", $text);
152 $text = preg_replace("#\<p\>\n ==(.*)\n\</p\>#iU", "\n<h2>$1</h2>", $text);
154 $text = preg_replace("#\<p\>\n \<ul\>(.*)\</ul\>\n\</p\>#iU", "<ul>\n $1\n</ul>", $text);
155 return $text;
156 }
158 ?>