使用 PHP 语法突出显示页面上的所有 ...($highlight_string() 不是一个选项)
我正在使用 CodeIgniter,以及 highlight_code("$string");
(更多信息)函数为动态站点提供语法突出显示。我希望用户能够提交自己以 BBCode 风格格式编写的帖子。我为此使用 NBBC PHP 库。
我的问题是,无论我如何做,我都无法让 NBBC 仅对用户输入的 [code][/code]
标签进行语法突出显示。这是 [code] 的 PHP:
'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',
),
如果您看到一行 {\$_content/v} 我在那里敲了敲,我认为这会突出显示标签中包含的代码。我不太记得这个输出是什么(我已经尝试了很多不同的组合),但我得到的最接近的是它从 PHP 导出为文本 - 在我的页面 XHTML 源中它只是显示为文本。
我该怎么做才能使页面上 标记内的所有内容都语法突出显示,最好使用highlight_code($string);或类似的?
我认为
标签之间的所有内容(不是 [code ],记住,但是 NBBC 输出的 HTML)。preg_replace
将是一个选项,但我不知道如何动态替换输出的
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).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
首先使用 preg_match 提取 [code][/code] 标签的内容,然后使用highlight_code() 提取该内容。
使用结果将原始字符串中的代码区域 preg_replace() 为突出显示的代码。伪代码:
希望它能起作用。
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:
Hope it works.