|
1 <?php |
|
2 /* |
|
3 * Decir |
|
4 * Version 0.1 |
|
5 * Copyright (C) 2007 Dan Fuhry |
|
6 * bbcode.php - BBcode-to-HTML renderer |
|
7 * |
|
8 * This program is Free Software; you can redistribute and/or modify it under the terms of the GNU General Public License |
|
9 * as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. |
|
10 * |
|
11 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied |
|
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for details. |
|
13 */ |
|
14 |
|
15 function str_replace_once($needle1, $needle2, $haystack) |
|
16 { |
|
17 $len_h = strlen($haystack); |
|
18 $len_1 = strlen($needle1); |
|
19 $len_2 = strlen($needle2); |
|
20 if ( $len_h < $len_1 ) |
|
21 return $haystack; |
|
22 if ( $needle1 == $haystack ) |
|
23 return $needle1; |
|
24 for ( $i = 0; $i < $len_h; $i++ ) |
|
25 { |
|
26 if ( substr($haystack, $i, $len_1) == $needle1 ) |
|
27 { |
|
28 $haystack = substr($haystack, 0, $i) . |
|
29 $needle2 . |
|
30 substr($haystack, $i + $len_1); |
|
31 return $haystack; |
|
32 } |
|
33 } |
|
34 } |
|
35 |
|
36 function render_bbcode($text, $bbcode_uid) |
|
37 { |
|
38 // First things first, strip out all [code] sections |
|
39 $text = decir_bbcode_strip_code($text, $bbcode_uid, $_code); |
|
40 |
|
41 // Bold text |
|
42 $text = preg_replace("/\[b:$bbcode_uid\](.*?)\[\/b:$bbcode_uid\]/is", '<b>\\1</b>', $text); |
|
43 |
|
44 // Italicized text |
|
45 $text = preg_replace("/\[i:$bbcode_uid\](.*?)\[\/i:$bbcode_uid\]/is", '<i>\\1</i>', $text); |
|
46 |
|
47 // Uunderlined text |
|
48 $text = preg_replace("/\[u:$bbcode_uid\](.*?)\[\/u:$bbcode_uid\]/is", '<u>\\1</u>', $text); |
|
49 |
|
50 // Colored text |
|
51 $text = preg_replace("/\[color=\#([A-F0-9]*){3,6}:$bbcode_uid\](.*?)\[\/color:$bbcode_uid\]/is", '<span style="color: #\\1">\\2</span>', $text); |
|
52 |
|
53 // Quotes |
|
54 $text = preg_replace("/\[quote:$bbcode_uid\](.*?)\[\/quote:$bbcode_uid\]/is", '<blockquote>\\1</blockquote>', $text); |
|
55 |
|
56 // Newlines |
|
57 $text = str_replace("\n", "<br />\n", $text); |
|
58 |
|
59 // Restore [code] blocks |
|
60 $text = decir_bbcode_restore_code($text, $bbcode_uid, $_code); |
|
61 |
|
62 // Code |
|
63 $text = preg_replace("/\[code:$bbcode_uid\](.*?)\[\/code:$bbcode_uid\]/is", '<pre>\\1</pre>', $text); |
|
64 |
|
65 return $text; |
|
66 } |
|
67 |
|
68 function decir_bbcode_strip_code($text, $uid, &$code_secs) |
|
69 { |
|
70 preg_match_all("/\[code:$uid\](.*?)\[\/code:$uid\]/is", $text, $matches); |
|
71 foreach ( $matches[1] as $i => $m ) |
|
72 { |
|
73 $text = str_replace_once($m, "{CODE_SECTION|$i:$uid}", $text); |
|
74 $code_secs[$i] = $m; |
|
75 } |
|
76 return $text; |
|
77 } |
|
78 |
|
79 function decir_bbcode_restore_code($text, $uid, $code_secs) |
|
80 { |
|
81 foreach ( $code_secs as $i => $code ) |
|
82 { |
|
83 $text = str_replace("{CODE_SECTION|$i:$uid}", $code, $text); |
|
84 } |
|
85 return $text; |
|
86 } |
|
87 |
|
88 function bbcode_strip_uid($bbcode, $uid) |
|
89 { |
|
90 // BBcode tags with attributes |
|
91 $bbcode = preg_replace("/\[([a-z]+?):{$uid}=([^\]]+?)\](.*?)\[\/\\1:{$uid}\]/is", '[\\1=\\2]\\3[/\\1]', $bbcode); |
|
92 |
|
93 // BBcode tags without attributes |
|
94 $bbcode = preg_replace("/\[([a-z]+?):{$uid}\](.*?)\[\/\\1:{$uid}\]/is", '[\\1]\\2[/\\1]', $bbcode); |
|
95 |
|
96 return $bbcode; |
|
97 } |
|
98 |