douxiajia6309 2010-02-22 20:24
浏览 60
已采纳

使用PHP语法突出显示页面上的所有<code> ...($ highlight_string()不是一个选项)

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' => "
<div class=\"bbcode_code\">
<p>Code:</p>
<code><?php $highlight_code(\"?>{\$_content/v}<?php \");?></code>
</div>
",
'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' => '
<div class=\"bbcode_code\">
<p>Code:</p>
<code>',
'simple_end'   => '</code>
</div>
',
),

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).

  • 写回答

1条回答 默认 最新

  • dozpv84422 2010-02-22 20:40
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。