bbcode
Posted: Sat Jul 26, 2008 10:52 pm
I am working on a bbcode class, and the basic replacements ([ b ], [ i ] , ect...) are easy enough to implement:
However, I am trying to add a syntax highlighter to parse [ code=xxx ] tags. For the syntax highlighting I am using GeSHi class. I figured doing the following would work:';echo $bbcode->parse($text);?>
Now it works to an extent, but it does not highlight the syntax (and it assumes the code is one line). There is no issue with the formatCode() function. Could it be possible that the $2 and $1 are being inteperated as literals when they get passed to the formatCode() function rather than what they reference? What would you suggest as an alternative solution?
Thanks,
John
Code: Select all
<?php
class BBcode
{
public function parse ($text)
{
$bbcode = array(
'bold' => "(\[b\](.+?)\[\/b])is");
$replace = array(
'bold' => '<strong>$1</strong>');
$text = preg_replace($bbcode, $replace, $text);
return $text;
}
}
$bbcode = new BBcode();
$text = '[ b ]this text is bold[/ b ]';
echo $bbcode->parse($text);
?>However, I am trying to add a syntax highlighter to parse [ code=xxx ] tags. For the syntax highlighting I am using GeSHi class. I figured doing the following would work:
Code: Select all
<?php
class BBcode
{
public function parse ($text)
{
$text = htmlentities($text);
$bbcode = array(
'bold' => "(\[b\](.+?)\[\/b])is" ,
'code' => "(\[code\=([a-zA-Z]*)\](.+?)\[\/code])is");
$replace = array(
'bold' => '<strong>$1</strong>' ,
'code' => $this->formatCode('$2', '$1'));
$text = preg_replace($bbcode, $replace, $text);
return $text;
}
public function formatCode ($code, $type)
{
require_once ('geshi.php');
$geshi = new HELPERS_GeSHi($code, $type);
$geshi->enable_line_numbers(1);
$code = $geshi->parse_code();
return $code;
}
}
$bbcode = new BBcode();
$text = '[b]some bold text[/b]Code: Select all
<?php echo "This is some code";
echo "line2";
$var = "hello world";
?>Now it works to an extent, but it does not highlight the syntax (and it assumes the code is one line). There is no issue with the formatCode() function. Could it be possible that the $2 and $1 are being inteperated as literals when they get passed to the formatCode() function rather than what they reference? What would you suggest as an alternative solution?
Thanks,
John