user966582
user966582

Reputation: 3313

Using syntax highligher with code tag?

I get some html output as:

<div class="test-code">    
   <pre>
       <code>
          <?php echo "hello world"; ?>
       </code>
    </pre>
</div>

Is it possible to use any basic PHP syntax highlighter with above HTML without changing anything in above syntax? I have been searching, but most of the syntax highlighters seems to be pretty advanced and require to define the code class in the pre (eg. pre class="php").

Edit: I am looking for any Javascript/jQuery based syntax highlight which automatically assumes that any code inside the <code> is a PHP code and highlights it.

Upvotes: 0

Views: 485

Answers (2)

Nick Beranek
Nick Beranek

Reputation: 2751

I would certainly recommend prettify.

It normally auto-detects the language being written in the code block.

Common usage is as follows:

<pre class="prettyprint">
  <code>
// your code here
  </code>
</pre>

NOTE: Prettify takes tabs literally. Do not tab your code within the block, but rather use spaces.

Upvotes: 1

Mike B
Mike B

Reputation: 32145

There's highlight_string() but that really only highlights the PHP code and wraps everything else in <code> tags

<?php

$markup = <<<MARKUP
<div class="test-code">    
   <pre>
       <code>
          <?php echo "hello world"; ?>
       </code>
    </pre>
</div>
MARKUP;

highlight_string($markup);

Output: http://codepad.org/qnLGqP7N

As far as I know, the method you mentioned is used by javascript highlighters.

Upvotes: 2

Related Questions