|
|
|
เรียกใช้ bbcode ยังไงคับ พอดีเห็นของคุณดุนยาเขียนไว้ ผมจะเรียกใช้มันยังไง ?? |
|
|
|
|
|
|
|
Code (PHP)
<?php
class bbcode
{
/**
* html list tyle
*
* @var array
*/
private $listStyle;
/**
* syntag of syntaxHighlighter
*
* @var array
*/
private $syntaxHighlighter;
/**
* custom BBcode
*
* @var array
*/
private $customBBCode;
/**
* html Replacement
*
* @var string
*/
private $htmlReplacement;
/**
* tag
*
* @var string
*/
private $tag;
/**
* remove message in code tag
*
* @var array
*/
private $removeMSGCodeTag = array('<br />','<br>');
/**
* substr long url
*
* @var boolean
*/
public $trimLongURLTag = false;
/**
* class constructor
*
* @return void
*/
public function __construct()
{
$this->listStyle = array('1' => '1','a' => 'a','A' => 'A', 'i' => 'i', 'I' => 'I');
$this->syntaxHighlighter = array(
'php' => 'php',
'js' => 'js',
'asp' => 'asp',
'sql' => 'sql',
'css' => 'css',
'xml' => 'xml',
'java' => 'java
');
$this->customBBCode = array(
'1' => array(
'b' => '<strong>{message}</strong>',
'i' => '<em>{message}</em>',
'u' => '<u>{message}</u>',
'left' => '<div style="text-align: left">{message}</div>',
'center'=> '<div style="text-align: center">{message}</div>',
'right' => '<div style="text-align: right">{message}</div>'
),
'2' => array(
'color' => '<span style="color: {option}">{message}</span>',
'size' => '<font size={option}>{message}</font>',
'quote' => "
<div class='quotemessage'>
<table width='90%' border='0' cellpadding='0' cellspacing='0'>
<tr>
<td>
<div class='quote_title' style='padding:5px; margin:0px;'><b>ข้อความโดย :: {option}</b></div>
<div class='quotecontent' style='padding:5px; margin:0px;'>{message}</div>
</td>
</tr>
</table>
</div>"
)
);
}
/**
* array to remove message in code text
*
* @param array $removeMSGCodeTag (array('<br />','\n'))
*
* @return void
*/
public function setremoveMSGCodeTag($removeMSGCodeTag)
{
$this->removeMSGCodeTag[] = $removeMSGCodeTag;
}
public function setTrimLongURLTag($boolean)
{
if ( is_bool($boolean) ) $this->trimLongURLTag = $boolean;
}
/**
* add new tag
*
* @param string $type (1-3)
* 1 : single tag
* 2 : double tag
* 3 : tripple tag
* @param string $tag ( video )
* @param string $html
*
* @return void
*/
public function addTag($type, $tag, $html)
{
$this->customBBCode[$type][$tag] = $html;
}
/**
* parse bbcode
*
* @param string $text
*
* @return string
*/
final public function parse($text)
{
if ( count( $this->customBBCode['1'] ) > 0 )
{
foreach ( $this->customBBCode['1'] AS $tag => $html)
{
$this->htmlReplacement = $html;
$this->tag = $tag;
$text = $this->customTag( $text );
}
}
if ( count( $this->customBBCode['2'] ) > 0 )
{
foreach ( $this->customBBCode['2'] as $tag => $html)
{
$this->htmlReplacement = $html;
$this->tag = $tag;
$text = $this->customOptionTag( $text );
}
}
$text = self::emailTag($text);
$text = $this->imgTag($text);
$text = $this->urlTag($text);
$text = $this->listTag($text);
$text = $this->codeTag($text);
return $text;
}
/**
* parse img tag
*
* @param string $text
*
* @return void
*/
public static function imgTag( $text )
{
return preg_replace('#\[img](.+?)\[/img\]#is', '<img src=\'\\1\' alt="" title="" />', $text);
}
/**
* parse url tag
*
* @param string $text
*
* @return string
*/
protected function urlTag( $text )
{
$text = preg_replace_callback('#\[url\](.+?)\[/url\]#is', array( &$this, 'urlTagCallBack' ), $text);
$text = preg_replace_callback('#\[link\](.+?)\[/link\]#is', array( &$this, 'urlTagCallBack' ), $text);
$text = preg_replace_callback('#\[url=(.+?)\](.+?)\[/url\]#is', array( &$this, 'urlOptionTagCallBack' ), $text);
$text = preg_replace_callback('#\[link=(.+?)\](.+?)\[/link\]#is', array( &$this, 'urlOptionTagCallBack' ), $text);
return $text;
}
/**
* parse url tag callback
*
* @param string $match
*
* @return string
*/
protected function urlTagCallBack( $match )
{
if ( mb_strlen($match[1],'utf-8') > 50 && $this->trimLongURLTag )
{
$start = mb_substr( $match[1], 0, 40,'utf-8' );
$end = mb_substr( $match[1], -10, 10,'utf-8');
$url = $start . '...' . $end;
}
else
{
$url = $match[1];
}
return '<a href="' . $match[1] . '" alt="" title="" target="_blank">' . $url . '</a>';;
}
/**
* parse url with option tag callback
*
* @param string $match
*
* @return string
*/
protected function urlOptionTagCallBack( $match )
{
if ( mb_strlen($match[2],'utf-8') > 50 && $this->trimLongURLTag && $match[1] == $match[2] )
{
$start = mb_substr( $match[2], 0, 40,'utf-8');
$end = mb_substr( $match[2], -10, 10,'utf-8');
$show = $start . '...' . $end;
}
else
{
$show = $match[2];
}
return '<a href="' . $match[1] . '" alt="" title="" target="_blank">' . $show . '</a>';;
}
/**
* parse url tag
*
* @param string $text
*
* @return string
*/
public static function emailTag( $text )
{
$text = preg_replace('#\[email](.+?)\[/email\]#is', '<a href="mailto:\'\\1\'" alt="" title="">\\1</a>', $text);
return $text;
}
/**
* parse custom tag
*
* @param string $text
*
* @return string
*/
protected function customTag( $text )
{
while( preg_match( "#\[" . $this->tag . "\](.+?)\[/" . $this->tag . "\]#is" , $text ) )
{
$text = preg_replace_callback( "#\[" . $this->tag . "\](.+?)\[/" . $this->tag . "\]#is" , array( &$this, 'customTagCallBack' ), $text );
}
return $text;
}
/**
* parse custom tag callback
*
* @param string $match
*
* @return string
*/
protected function customTagCallBack( $match )
{
return str_replace('{message}' ,$match[1], $this->htmlReplacement);
}
/**
* parse custom option tag
*
* @param string $text
*
* @return string
*/
protected function customOptionTag( $text )
{
while( preg_match( "#\[" . $this->tag . "=(.+?)\](.+?)\[/" . $this->tag . "\]#is" , $text ) )
{
$text = preg_replace_callback( "#\[" . $this->tag . "=(.+?)\](.+?)\[/" . $this->tag . "\]#is" , array( &$this, 'customOptionTagCallBack' ), $text );
}
return $text;
}
/**
* parse custom option tag callback
*
* @param string $match
*
* @return string
*/
protected function customOptionTagCallBack( $match )
{
$text = str_replace('{message}' ,$match[2], $this->htmlReplacement);
return str_replace('{option}' ,$match[1], $text);
}
/**
* parse list with option tag
*
* @param string $text
*
* @return string
*/
protected function listTag( $text )
{
while ( preg_match( "#\[list\](.+?)\[/list\]#is" , $text ) )
{
$text = preg_replace_callback('#\[list\](.+?)\[/list\]#is', array( &$this, 'listTagCallBack' ), $text);
}
while ( preg_match( "#\[list=(.+?)\](.+?)\[/list\]#is" , $text ) )
{
$text = preg_replace_callback('#\[list=(.+?)\](.+?)\[/list\]#is', array( &$this, 'listOptionTagCallBack' ), $text);
}
return $text;
}
/**
* parse list with option tag callback
*
* @param string $match
*
* @return string
*/
protected function listTagCallBack( $match )
{
while ( preg_match( "#\[li\](.+?)\[/li\]#is" , $match[1] ) )
{
$match[1] = preg_replace('#\[li\](.+?)\[/li\]#is', '<li>\\1 \\2</li>', $match[1]);
}
return '<ol style="list-style: disc">' . $match[1] . '</ol>';
}
/**
* parse list with option tag callback
*
* @param string $match
*
* @return string
*/
protected function listOptionTagCallBack( $match )
{
while ( preg_match( "#\[li\](.+?)\[/li\]#is" , $match[2] ) )
{
$match[2] = preg_replace( '#\[li\](.+?)\[/li\]#is' , '<li>\\1 \\2</li>', $match[2]);
}
return '<ol type="' . $this->listStyle[$match[1]] . '">' . $match[2] . '</ol>';
}
/**
* parse code tag
*
* @param string $text
*
* @return string
*/
protected function codeTag( $text )
{
return preg_replace_callback('#\[code=(.+?)\](.*?)\[/code\]#is', array( &$this, 'codeTagCallBack' ), $text);
}
/**
* parse code tag callback
*
* @param string $match
*
* @return string
*/
protected function codeTagCallBack( $match )
{
if ( count( $this->removeMSGCodeTag ) > 0 )
{
foreach ( $this->removeMSGCodeTag AS $value )
{
$match[2] = str_replace( $value,'',$match[2] );
}
}
return '<pre class="brush: ' . $this->syntaxHighlighter[$match[1]] . '">' . $match[2] . '</pre>';
}
}
class postMessageBox
{
private $tag = array();
private $toolWidth = '400';
private $textName = 'txtBox';
private $imgURL = 'images';
private $defaultValue = '';
public function __construct( $textName )
{
$this->textName = $textName;
$this->tag = array(
'1' => array(
'b' => array('ตัวหนา', 'bold.gif'),
'i' => array('ตัวเอียง', 'em.gif'),
'u' => array('ขีดเส้นใต้', 'underline.gif'),
'left' => array('จัดข้อความชิดซ้าย', 'left.gif'),
'center'=> array('จัดข้อความชิดกลาง', 'center.gif'),
'right' => array('จัดข้อความชิดขวา', 'right.gif'),
'img' => array('รูปภาพ', 'img.gif'),
'email' => array('อีเมล์', 'email.gif')
),
'2' => array(
'color' => array('สีข้อความ', 'color.gif', 'ป้อนสี'),
'quote' => array('อ้างอิงข้อความ', 'quote.gif', 'ใส่ชื่อเจ้าของข้อความ'),
'url' => array('ลิงค์', 'url.gif','ป้อนลิงค์'),
'code' => array('โค้ด', 'code.gif','ป้อนภาษาที่เขียน เช่น php, js, asp, sql, css')
),
'3' => array(
'list' => array('ลิส์แบบลำดับ','orderlist.gif','ป้อนประเภทลิสท์ เช่น 1, a, A, i, I','li','ป้อนรายการ')
)
);
}
public function setToolWidth( $width )
{
$this->toolWidth = $width;
}
public function setDefaultValue( $message )
{
$this->defaultValue = $message;
}
public function addTag($type, $tag, $title, $image, $message = '')
{
$this->tag[$type][$tag] = array( $title, $image, $message) ;
}
private function fetchBBCode()
{
if ( count($this->tag) > 0 )
{
foreach( $this->tag AS $type => $text )
{
switch ( $type )
{
case 1:
foreach( $text AS $tag => $message )
{
$list[1][] = <<<HTML
<a onclick="addBBCode(1,'$tag', '{$this->textName}'); return false;" href="javascript:void(0);"><img src="{$this->imgURL}/{$message[1]}" title="{$message[0]}" alt="{$message[0]}" border="0" /></a>
HTML;
}
break;
case 2:
foreach( $text AS $tag => $message )
{
$list[2][] = <<<HTML
<a onclick="addBBCode(2,'$tag', '{$this->textName}', '{$message[2]}'); return false;" href="javascript:void(0);"><img src="{$this->imgURL}/{$message[1]}" title="{$message[0]}" alt="{$message[0]}" border="0" /></a>
HTML;
}
break;
case 3:
foreach( $text AS $tag => $message )
{
$list[3][] = <<<HTML
<a onclick="addBBCode(3,'$tag', '{$this->textName}', '{$message[2]}', '{$message[3]}', '{$message[4]}'); return false;" href="javascript:void(0);"><img src="{$this->imgURL}/{$message[1]}" title="{$message[0]}" alt="{$message[0]}" border="0" /></a>
HTML;
}
break;
default:
$list[1][] = <<<HTML
<a onclick="addBBCode(1,'$tag', '{$this->textName}'); return false;" href="javascript:void(0);"><img src="{$this->imgURL}/{$message[1]}" title="{$message[0]}" alt="{$message[0]}" border="0" /></a>
HTML;
break;
}
}
return array($list[1],$list[2],$list[3]);
}
}
protected function getPostToolFormat()
{
list($list1, $list2, $list3) = $this->fetchBBCode();
if ( count($list1) > 0 )
{
$list .= "<tr>\n";
$list .= "<td>" . implode(" ", $list1) . "</td>\n";
$list .= "</tr>\n";
}
if ( count($list2) > 0 )
{
$list .= "<tr>\n";
$list .= "<td>" . implode(" ", $list2) . "</td>\n";
$list .= "</tr>\n";
}
if ( count($list3) > 0 )
{
$list .= "<tr>\n";
$list .= "<td>" . implode(" ", $list3) . "</td>\n";
$list .= "</tr>\n";
}
return <<<HTML
<table width="{$this->toolWidth}" cellpadding="0" cellspacing="0">
{$list}
</table>
HTML;
}
final public function getPostMessageBox()
{
$list = $this->getPostToolFormat();
return <<<HTML
<table width="{$this->toolWidth}" cellpadding="5" cellspacing="2">
<tr>
<td>
<div class="textarea">
<table width="98%" cellpadding="5" cellspacing="2">
<tr>
<td>
{$list}
</td>
</tr>
</table>
<table width="98%" cellpadding="5" cellspacing="2">
<tr>
<td>
<textarea cols="80" rows="20" name="{$this->textName}" id="{$this->textName}">{$this->defaultValue}</textarea>
</td>
</tr>
</table>
</div>
</td>
</tr>
</table>
HTML;
}
}
class textParse
{
private $parsenl2br = true;
private $parseBBCode = true;
private $autoParseURL = true;
private $stripHTML = true;
private $bbcode;
private $allowHtml = true;
public function __construct( $nl2br = true, $bbcode = true, $stripHTML = false, $allowHtml = false)
{
$this->bbcode = new bbcode();
if ( is_bool( $nl2br ) ) $this->parsenl2br = $nl2br;
if ( is_bool( $bbcode ) ) $this->parseBBCode = $bbcode;
if ( is_bool( $stripHTML ) ) $this->stripHTML = $stripHTML;
if ( is_bool( $allowHtml ) ) $this->allowHtml = $allowHtml;
}
final public function parseText( $text )
{
if ( $this->stripHTML ) $text = strip_tags( $text );
if ( !$this->allowHtml ) $text = htmlspecialchars( $text );
//$text = preg_replace( "/<br>|<br \/>/", "\n", $text );
# First we did hex, now we do url encoded
# <script
$text = str_replace( "%3C%73%63%72%69%70%74", "<script" , $text );
# document.cookie
$text = str_replace( "%64%6F%63%75%6D%65%6E%74%2E%63%6F%6F%6B%69%65", "document.cookie", $text );
$text = preg_replace( "#javascript\:#is" , "java script:" , $text );
$text = preg_replace( "#vbscript\:#is" , "vb script:" , $text );
$text = str_replace( "`" , "`" , $text );
$text = preg_replace( "#moz\-binding:#is" , "moz binding:" , $text );
$text = str_replace( "<script" , "<script" , $text );
$text = str_replace( "‮" , '' , $text );
$text = str_replace( "<?" , '<?' , $text );
if ( $this->autoParseURL )
{
$text = preg_replace_callback( "#(^|\s|>)((http|https|news|ftp)://\w+[^\s\[\]\<]+)#i", array( &$this, 'buildURLManualCallBack' ), $text );
}
if ( $this->parsenl2br )
$text = nl2br( $text );
else
$text = str_replace( "\n", "", $text );
if ( $this->parseBBCode ) $text = $this->bbcode->parse($text);
return $text;
}
protected function buildURLManualCallBack( $matches = array() )
{
$url = $matches[2] ? $matches[2] : $matches[1];
if ( mb_strlen($url, 'utf-8') > 50 && $this->bbcode->trimLongURLTag )
{
$start = mb_substr( $url, 0, 40, 'utf-8' );
$end = mb_substr( $url, -10, 10, 'utf-8');
$url = $start . '...' . $end;
}
return '<a href="' . ( $matches[2] ? $matches[2] : $matches[1] ) . '" target="_blank" >' . $url . '</a>';
}
}
?>
Tag : PHP, MySQL
|
|
|
|
|
|
Date :
2011-09-15 12:32:23 |
By :
lengza |
View :
1166 |
Reply :
2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
คุณ lekZa
ช่วยอธิบาย code ให้หน่อยนะครับ เพราะไม่เข้าใจ และการเรียกใช้ อย่างเช่น [font=Verdana]เรียกใช้[/font]http:// ให้ bbcode พิมพ์ลงใน textbox นะครับ
นับถือ
|
|
|
|
|
Date :
2011-12-08 10:34:12 |
By :
wat2 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 01
|