+ Reply to Thread
Results 1 to 2 of 2

Thread: syntax highlighting for PHP code

  1. #1
    dharmil's Avatar
    dharmil is offline x10 Elder dharmil is an unknown quantity at this point
    Join Date
    Sep 2005
    Location
    Avenel New Jersey
    Posts
    828

    syntax highlighting for PHP code

    i need help creating a bbcode that will higlight php and html code
    so it colored unlike regular CODE tag
    like
    [php ] code [/ php]
    and it will look like this:
    PHP Code:
    <?php
    echo "Hello World!";
    ?>
    and some for html
    [html ] code [/ html]
    HTML Code:
    <input type="text" name="from" size="24" />
    and it has to work with this:
    PHP Code:
     function bbcode($message){ 
         
    $search = array('#\[b\](.*?)\[/b\]#s',
                         
    '#\[img\](.*?)\[/img\]#s',
                         
    '#\[i\](.*?)\[/i\]#s',
                         
    '#\[u\](.*?)\[/u\]#s',
                         
    '#\[s\](.*?)\[/s\]#s',
                         
    '#\[q\](.*?)\[/q\]#s',
                         
    '#\[c\](.*?)\[/c\]#s',
                         
    '#\[url=(.*?)\](.*?)\[/url\]#s',
                         
    '#\[email=(.*?)\](.*?)\[/email\]#s',
                         
    '#\[color=([a-zA-Z]*|\#?[0-9a-fA-F]{6})\](.*?)\[/color\]#s',
                         
    '#\[---\]#s',
                         
    '#\[h\](.*?)\[/h\]#',
                         
    '#\[size=(.*?)\](.*?)\[/size\]#si',
                         
    '#\[font=(.*?)\](.*?)\[/font\]#',
                         
    '#\[align=(.*?)\](.*?)\[/align\]#',
                         
    '#\[style=(.*?)\](.*?)\[/style\]#',
                         
    '#\[code\](.+?)\[/code\]#s',
                         
    '#\[br\]#s' );
        
    $replace = array('<strong>$1</strong>',
                         
    '<a href="$1"><img src="http://dharmil.info/thumb.php?img=$1" border="0" /></a><br /><a href="$1">Click to Enlarge (+)</a><br />',
                         
    '<em>$1</em>',
                         
    '<ins>$1</ins>',
                         
    '<del>$1</del>',
                         
    '<q>$1</q>',
                         
    '<code>$1</code>',
                         
    '<a href="$1">$2</a>',
                         
    '<a href="mailto:$1">$2</a>',
                         
    '<span style="color: $1">$2</span>',
                         
    '<hr />',
                         
    '<span style="background-color: #FFFF00; color: #000000">$1</span>',
                         
    '<font size=\'$1\'>$2</font>',
                         
    '<span style="font-family: $1">$2</span>',
                         
    '<div align="$1">$2</div>',
                         
    '<span style="$1">$2</span>',
                         
    '<div class="code-top"><strong>Code:</strong></div><div class="code"><div class="codeinside">$1</div></div>',
                         
    '<br />' );
         
    $bbcodeize ''.preg_replace($search$replace$message).'';
        return 
    $bbcodeize;

    Last edited by dharmil; 08-03-2006 at 12:38 PM.

  2. #2
    The_Magistrate's Avatar
    The_Magistrate is offline x10 Elder The_Magistrate is an unknown quantity at this point
    Join Date
    May 2005
    Location
    PA
    Posts
    559

    Re: syntax highlighting for PHP code

    I think you're looking for [url=http://www.php.net/manual/en/function.highlight-string.php]highlight_string()[url]. You can use it to highlight a string of PHP in an echo. You would need something like this:

    PHP Code:
    function bbcode($message){ 
         
    $search = array('#\[b\](.*?)\[/b\]#s',
                         
    '#\[img\](.*?)\[/img\]#s',
                         
    '#\[i\](.*?)\[/i\]#s',
                         
    '#\[u\](.*?)\[/u\]#s',
                         
    '#\[s\](.*?)\[/s\]#s',
                         
    '#\[q\](.*?)\[/q\]#s',
                         
    '#\[c\](.*?)\[/c\]#s',
                         
    '#\[url=(.*?)\](.*?)\[/url\]#s',
                         
    '#\[email=(.*?)\](.*?)\[/email\]#s',
                         
    '#\[color=([a-zA-Z]*|\#?[0-9a-fA-F]{6})\](.*?)\[/color\]#s',
                         
    '#\[---\]#s',
                         
    '#\[h\](.*?)\[/h\]#',
                         
    '#\[size=(.*?)\](.*?)\[/size\]#si',
                         
    '#\[font=(.*?)\](.*?)\[/font\]#',
                         
    '#\[align=(.*?)\](.*?)\[/align\]#',
                         
    '#\[style=(.*?)\](.*?)\[/style\]#',
                         
    '#\[code\](.+?)\[/code\]#s',
                         
    '#\[br\]#s',
                         
    '#\[php\](.+?)\[/php\]#s' );
        
    $replace = array('<strong>$1</strong>',
                         
    '<a href="$1"><img src="http://dharmil.info/thumb.php?img=$1" border="0" /></a><br /><a href="$1">Click to Enlarge (+)</a><br />',
                         
    '<em>$1</em>',
                         
    '<ins>$1</ins>',
                         
    '<del>$1</del>',
                         
    '<q>$1</q>',
                         
    '<code>$1</code>',
                         
    '<a href="$1">$2</a>',
                         
    '<a href="mailto:$1">$2</a>',
                         
    '<span style="color: $1">$2</span>',
                         
    '<hr />',
                         
    '<span style="background-color: #FFFF00; color: #000000">$1</span>',
                         
    '<font size=\'$1\'>$2</font>',
                         
    '<span style="font-family: $1">$2</span>',
                         
    '<div align="$1">$2</div>',
                         
    '<span style="$1">$2</span>',
                         
    '<div class="code-top"><strong>Code:</strong></div><div class="code"><div class="codeinside">$1</div></div>',
                         
    '<br />',
                         
    highlight_string($1) );
         
    $bbcodeize ''.preg_replace($search$replace$message).'';
        return 
    $bbcodeize;

    You may have to tweak the above code, because I haven't tested it, but it should work.
    Getting Started | Terms of Service | Paid Hosting | Forum Rules | Free Server Status | Banned Countries

    If I have helped you through one of my posts, please click the
    blue checkbox on the right below my avatar to add to my reputation.

+ Reply to Thread

Similar Threads

  1. Hybrid's HTML Lessons
    By Hybrid in forum Tutorials
    Replies: 18
    Last Post: 11-28-2009, 02:12 PM
  2. how can i convert Php pages and code into asp pages and code
    By n4tec in forum Scripts & 3rd Party Apps
    Replies: 11
    Last Post: 01-10-2009, 09:40 PM
  3. Ad Code Question
    By reiterb in forum Free Hosting
    Replies: 1
    Last Post: 08-18-2005, 09:29 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers