<?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 />','\n');
/**
* 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(
'li' => '<li>'
),
'2' => array(
'b' => '<strong>{message}</strong>',
'i' => '<em>{message}</em>',
'u' => '<u>{message}</u>'
),
'3' => array(
'color' => '<span style="color: {option}">{message}</span>',
'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;
}
/**
* add new tag
*
* @param string $type (s, d, t)
* 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)
{
$text = preg_replace('#\['.$tag.'\]#si', '<'.$tag.' />', $text);
}
}
if ( count( $this->customBBCode['2'] ) > 0 )
{
foreach ( $this->customBBCode['2'] AS $tag => $html)
{
$this->htmlReplacement = $html;
$this->tag = $tag;
$text = $this->customTag( $text );
}
}
if ( count( $this->customBBCode['3'] ) > 0 )
{
foreach ( $this->customBBCode['3'] as $tag => $html)
{
$this->htmlReplacement = $html;
$this->tag = $tag;
$text = $this->customOptionTag( $text );
}
}
$text = self::imgTag($text);
$text = self::urlTag($text);
$text = self::emailTag($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\]#si', '<img src=\'\\1\' alt="" title="" />', $text);
}
/**
* parse url tag
*
* @param string $text
*
* @return string
*/
public static function urlTag( $text )
{
$text = preg_replace('#\[url=(.*?)\](.*?)\[/url\]#si', '<a href=\'\\1\' alt="" title="">\\2</a>', $text);
$text = preg_replace('#\[link=(.*?)\](.*?)\[/link\]#si', '<a href=\'\\1\' alt="" title="">\\2</a>', $text);
return $text;
}
/**
* parse url tag
*
* @param string $text
*
* @return string
*/
public static function emailTag( $text )
{
$text = preg_replace('#\[email](.*?)\[/email\]#si', '<a href="mailto:\'\\1\'" alt="" title="">\\1</a>', $text);
return $text;
}
/**
* parse custom tag
*
* @param string $text
*
* @return string
*/
public function customTag( $text )
{
return preg_replace_callback('#\[' . $this->tag . '\](.*?)\[/' . $this->tag . '\]#si', array( &$this, 'customTagCallBack' ), $text);
}
/**
* parse custom tag callback
*
* @param string $match
*
* @return string
*/
public function customTagCallBack( $match )
{
return str_replace('{message}' ,$match[1], $this->htmlReplacement);
}
/**
* parse custom option tag
*
* @param string $text
*
* @return string
*/
public function customOptionTag( $text )
{
return preg_replace_callback('#\[' . $this->tag . '=(.*?)\](.*?)\[/' . $this->tag . '\]#si', array( &$this, 'customOptionTagCallBack' ), $text);
}
/**
* parse custom option tag callback
*
* @param string $match
*
* @return string
*/
public function customOptionTagCallBack( $match )
{
$text = str_replace('{message}' ,$match[2], $this->htmlReplacement);
return str_replace('{option}' ,$match[1], $text);
}
/**
* parse list tag
*
* @param string $text
*
* @return string
*/
public function listTag( $text )
{
return preg_replace_callback('#\[list=(.*?)\](.*?)\[/list\]#si', array( &$this, 'listTagCallBack' ), $text);
}
/**
* parse list tag callback
*
* @param string $match
*
* @return string
*/
public function listTagCallBack( $match )
{
return '<ol type="' . $this->listStyle[$match[1]] . '">' . $match[2] . '</ol>';
}
/**
* parse code tag
*
* @param string $text
*
* @return string
*/
public function codeTag( $text )
{
return preg_replace_callback('#\[code=(.*?)\](.*?)\[/code\]#si', array( &$this, 'codeTagCallBack' ), $text);
}
/**
* parse code tag callback
*
* @param string $match
*
* @return string
*/
public 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>';
}
}[/code]
คือ ถ้า bbcode ไม่ซ้อนกัน ก็ไม่มีปัญหา แต่มันซ้อนกันเมือไร จบเลย
ผมใช้แบบนี้อะ
/**
* parse custom tag
*
* @param string $text
*
* @return string
*/
public function customTag( $text )
{
return preg_replace_callback('#\[' . $this->tag . '\](.*?)\[/' . $this->tag . '\]#si', array( &$this, 'customTagCallBack' ), $text);
}
/**
* parse custom tag callback
*
* @param string $match
*
* @return string
*/
public function customTagCallBack( $match )
{
return str_replace('{message}' ,$match[1], $this->htmlReplacement);
}
?>
สมมุติ
Code
[code][quote='danya']ผมเอง[/quote]
มันไม่มีปัญหา
แต่ถ้า
Code
[quote='num'][quote='danya']ผมเอง[/quote]คุณเองเหรอ[/quote]
จะมีปัญหาทันที
[ quote='num'] เพราะมันไปตัด [ /quote] อันแรก มันไม่ยอมไปตัด [ /quote] อันหลัง
อธิบายอีกที มันจะมองแบบนี้
Code
[quote='num'][quote='danya']ผมเอง[/quote]
แทนที่จะมองแบบนี้
Code
[quote='num'][quote='danya']ผมเอง[/quote]คุณเองเหรอ[/quote]
ผมจะใช้การตรวจสอบอย่างไรดีอะ
Code
reg_replace_callback('#\[' . $this->tag . '\](.*?)\[/' . $this->tag . '\]#si', array( &$this, 'customTagCallBack' ), $text);
ผมว่า ต้องแก้ไขตรงบันทัดนี้ แต่ยังคิดไม่ออกว่า จะแก้ไขยังงัย เหอๆ
Tag : - - - -