Reputation: 9784
I'm using CodeIgniter, alongside the highlight_code("$string");
(More info) function to provide syntax highlighting to a dynamic site. I want the users to be able to submit their own posts written in a BBCode-style format. I'm using NBBC PHP library for this.
My problem is that nomatter how I do it I cannot get NBBC to syntax-highlight only [code][/code]
tags that my users enter. Here's the PHP for [code]:
'code' => Array(
'mode' => BBCODE_MODE_ENHANCED,
'template' => "\n<div class=\"bbcode_code\">\n<p>Code:</p>\n<code><?php $highlight_code(\"?>{\$_content/v}<?php \");?></code>\n</div>\n",
'class' => 'code',
'allow_in' => Array('listitem', 'block', 'columns'),
'content' => BBCODE_VERBATIM,
'before_tag' => "sns",
'after_tag' => "sn",
'before_endtag' => "sn",
'after_endtag' => "sns",
'plain_start' => "<div id=\"footer\">",
'plain_end' => "</div>",
'simple_start' => '\n<div class=\"bbcode_code\">\n<p>Code:</p>\n<code>',
'simple_end' => '</code>\n</div>\n',
),
If you see the line which says <?php $highlight_code(\"?>{\$_content/v}<?php \");?>
that I whacked in there, I thought that would highlight the code contained within the tags. I can't quite remember what this outputs (I've tried plenty of different combinations) but the closest I got was it being exported as text from PHP - in my page XHTML source it just appeared as text.
What can I do to make all content within tags on a page be syntax-highlighted, ideally using highlight_code($string); or similar?
I was thinking preg_replace
would be an option but I don't know how to do it to dynamically replace everything between outputted tags (not [code], remember, but HTML which NBBC outputs).
Upvotes: 1
Views: 1379
Reputation: 10091
First use preg_match to extract the contents of the [code][/code] tags, and highlight_code() that.
Use the results to preg_replace() the code area in the original string with the highlighted code. Pseudocode-ish:
$code = preg_match(/code pattern/, $string);
$code = highlight_code($code);
$string = preg_replace(/code pattern/, $code, $string);
Hope it works.
Upvotes: 2