Page 1 of 1

Custom BBcode to pass to a script.

Posted: Sat Dec 11, 2004 12:37 pm
by qwest
OK, I am allowing my users to submit articles to my site. This is a web developers site so of course their articles will sometimes need to contain code. I am using the GeSHi syntax highliter class so highlighting is not a problem. The problem is I want to allow my users to use BBcode just like SitePoint does for PHP code to pass the code to GeSHi for highlighting.

PLEASE NOTE: I already know about the PHP highlight function "for PHP Code". I need to use BBcode because the site will support many languages, Actionscript, Javascript, CSS, PHP, ASP, etc.

So for each language I want to have a BBcode for it like

[*ASP][*/ASP]
[*PHP][*/PHP]
[*CSS][*/CSS]
[*JS][*/JS]
(without the * of course)

So when a user submits a post, and the $postBody variable looks like say:

Code: Select all

$postBody = 'Some text describing the code. 
Then some more text almost a paragraph worth. 
OK here's an example: 

[*PHP]$foo = 45;
for ( $i = 1; $i < $foo; $i++ )
{
  echo "$foo<br />\n";
  --$foo;
}[*/PHP]

Now more text yadda yadda. 
Did you like my example, 
yadda yadda yadda.';
I need to send $postBody to a BBcode parser and grab everything inside [*PHP][*/PHP] to send to GeShi in the following format:

Code: Select all

$lang = 'PHP';  <-- I need it to recognize the tag name
$source = 'the code here'; <-- take only the code between the tags
I only need those 2 things because I pass it to GeSHi like so:

Code: Select all

$geshi = new GeSHi($source, $lang, $path);
^^ path is just the path to GeSHi

I can echo it back like this:

Code: Select all

echo $geshi->parse_code();
So the question is....
How do I grab the data already inside $postBody in the BBcode tags along with the tag identifier in those 2 variables... then... after I pass it to GeSHi, echo $postBody with the GeShI formatted code?

Posted: Sat Dec 11, 2004 12:45 pm
by John Cartwright
This can be very easily done, search these forums as it has been discussed more times than I can count on my fingers. Not to mention, you can look at phpBB bbcode functions.

Helpful links can be found at [php_man]preg_match[/php_man], [devnet]bbcode[/devnet]

Posted: Sat Dec 11, 2004 1:27 pm
by timvw
last time i did it, i used: http://pear.php.net/package/HTML_BBCodeParser ... and added my own extension: http://home.mysth.be/~timvw/programming ... source.txt

i've been thinking about writing the same with geshi... but untill now haven't found the time ;)