BBCode w PHP
Zapewne wielu z Was zastanawia się jak dodać tagi BBCode do swoich księg gości, for etc. Otóż jest to bardzo proste i wystarczy tylko kilka linijek kodu. Wszystko będzie się operać na preg_replace
Listing
$tresc=str_replace(" ", '  ', nl2br(htmlspecialchars($tresc)));
$tresc=preg_replace("#\[u\](.*?)\[/u\]#si", "<u>\\1</u>", $tresc);
$tresc=preg_replace("#\[img\](.*?)\[/img\]#si", "<img src='\\1' border='0' alt='obrazek' />", $tresc);
$tresc=preg_replace("#\[url=(.*?)\](.*?)\[/url\]#si", "<a href='\\1'>\\2</a>", $tresc);
$tresc=preg_replace("#\[b\](.*?)\[/b\]#si", "<b>\\1</b>", $tresc);
$tresc=preg_replace("#\[i\](.*?)\[/i\]#si", "<i>\\1</i>", $tresc);
$tresc=preg_replace("#\[color=(.*?[^'])\](.*?)\[/color\]#si", "<font color='\\1'>\\2</font>", $tresc);
$tresc=preg_replace("#\[u\](.*?)\[/u\]#si", "<u>\\1</u>", $tresc);
$tresc=preg_replace("#\[img\](.*?)\[/img\]#si", "<img src='\\1' border='0' alt='obrazek' />", $tresc);
$tresc=preg_replace("#\[url=(.*?)\](.*?)\[/url\]#si", "<a href='\\1'>\\2</a>", $tresc);
$tresc=preg_replace("#\[b\](.*?)\[/b\]#si", "<b>\\1</b>", $tresc);
$tresc=preg_replace("#\[i\](.*?)\[/i\]#si", "<i>\\1</i>", $tresc);
$tresc=preg_replace("#\[color=(.*?[^'])\](.*?)\[/color\]#si", "<font color='\\1'>\\2</font>", $tresc);
Dodano przez: eider Ranga: 0 Punktów: 0
Komentarze użytkowników
klasa bbcode
- Zobacz kod dodany do tego wpisu
-
<?php
/**** USER == ALL ****/
/********
BBCODE.CLASS.PHP - A BBcode class
@author Robin Erixon (robin@robinerixon.se)
@copyright Robin Erixon - 2008
@version 1.0
This class may be used as long as this comment section is intact.
Available tags:
[b] [/b] = Bold.
[i] [/i] = Italic.
[color= ] [/color] = Colored.
[url] [/url] = Link type 1.
[url= ] [/url] = Link typ 2.
[youtube= ] [/youtube] = Embedded Youtube player.
[google] [/google] = Link direct to search result.
[quote] [/quote] = A quote.
[img] [/img] = Shows an image.
[code] [/code] = Simple code box.
[highlight] [/highlight] = Highlight a word or a piece of text.
[size= ] [/size] = Change size of text
********/
class BBcode
{
/**************************** SETTINGS *****************************/
// Choose default active tags.
private $activeTags = array(
'b' => true,
'i' => true,
'color' => true,
'highlight' => true,
'size' => true,
'color=' => true,
'smiley' => true,
'quote' => true,
'img' => true,
'youtube' => true,
'code' => true,
'url' => true,
'url=' => true
);
// Choose pre-defined colors. The user writes a colorname that translates into a color(hex)
private $preDefinedColors = array(
'black' => '000000',
'white' => 'ffffff',
'green' => 'cad5a3'
);
// Choose pre-defined smilies.The user writes a smiley and that translates into a image name.
private $preDefinedSmilies = array(
':smile:' => 'Smilies/smile.png',
':sad:' => 'Smilies/sad.png',
':love:' => 'Smilies/love.png'
);
/************************* END OF SETTINGS **************************/
public function tagState($tag, $state) {
$this->activeTags[$tag] = $state;
}
public function exec($str) {
// Protect against evil code
$str = htmlentities($str);
// Bold
if ($this->activeTags['b'] === true) {
$str = preg_replace('#[b](.*?)[/b]#is', '<strong>1</strong>', $str);
}
// Italic
if ($this->activeTags['i'] === true) {
$str = preg_replace('#[i](.*?)[/i]#is', '<i>1</i>', $str);
}
// Colored
if ($this->activeTags['color'] === true) {
$str = preg_replace('#[color=(#([a-f0-9]){3,6})](.*?)[/color]#is', '<span style="color: 1;">3</span>', $str);
}
// Highlight
if ($this->activeTags['highlight'] === true) {
$str = preg_replace('#[highlight](.*?)[/highlight]#is', '<span class="highlight">1</span>', $str);
}
// Font size
if ($this->activeTags['size'] === true) {
$str = preg_replace('#[size=(7|9|12|18|24)](.*?)[/size]#is', '<span style="font-size: 1px">2</span>', $str);
}
// Pre defenied colors
if ($this->activeTags['color='] === true) {
foreach ($this->preDefinedColors as $color => $hex) {
$str = preg_replace('#[color=(' . $color . ')](.*?)[/color]#is', '<span style="color: #' . $hex . ';">2</span>', $str);
}
}
// Smilies
if ($this->activeTags['smiley'] === true) {
foreach ($this->preDefinedSmilies as $code => $smiley) {
$str = preg_replace('#(' . $code . ')#is', '<img src="' . $smiley . '" alt="Smiley" />', $str);
}
}
// Quote
if ($this->activeTags['quote'] === true) {
$str = preg_replace('#[quote](.*?)[/quote]#is', '<div class="quoteDiv"><p>1</p></div><p>', $str);
}
// Image
if ($this->activeTags['img'] === true) {
$str = preg_replace('#[img]((http://)?)(.*?)[/img]#ise', "'$1' != '' ? '<img src="$1$3" alt="$1$3" />' : '<img src="http://$3" alt="http://$3" />'", $str);
}
// Youtube
if ($this->activeTags['youtube'] === true) {
$str = preg_replace('#[youtube](.*?)[/youtube]#is', '<object width="425" height="355"><param name="movie" value="http://www.youtube.com/v/1&hl=en"></param><param name="wmode" value="transparent"></param><embed src="http://www.youtube.com/v/1&hl=en" type="application/x-shockwave-flash" wmode="transparent" width="425" height="355"></embed></object>', $str);
}
// Code
if ($this->activeTags['code'] === true) {
$str = preg_replace('#[code](.*?)[/code]#is', '</p><div class="codeDiv"><p>1</p></div><p>', $str);
}
// URL 1
if ($this->activeTags['url'] === true) {
$str = preg_replace('#[url](http://)?(.*?)[/url]#ise', "'$1' != '' ? '<a href="$1$2">$1$2</a>' : '<a href="http://$2">$2</a>'", $str);
}
// URL 2
if ($this->activeTags['url='] === true) {
$str = preg_replace('#[url=(http://)?(.*?)](.*?)[/url]#ise', "'$1' != '' ? '<a href="$1$2">$3</a>' : '<a href="http://$2">$3</a>'", $str);
}
// Line breaks
$str = nl2br($str);
return $str;
}
}
?>
-
autor: igor | 5434 | 2010-03-16 21:21:48
:: Losowe artykuły
:: Wymiana linków
Modowe inspiracje |
Android Gry i Aplikacje |
ZaplanujTransport.pl: Przeprowadzki, transport, aukcje |
Logo dla firmy |
Change Tires - Car Weather Forecast Reminder |
Laminas: MVC Framework for PHP |
IT Books Reviews and Programming: JS, JAVA, PHP, ANDROID, CSS |
Katalog roślin |
Programming articles: JAVA, PHP, C++, Python, JavaScript |
Kancelaria Adwokacka Łukasz Huszno