www.webdeveloper.com
Results 1 to 6 of 6

Thread: code highlighting

  1. #1
    Join Date
    Jan 2005
    Posts
    592

    code highlighting

    Hi,

    I am looking for a class which can highlight code. I want to provide a sting (could be an entire file) and I want to get the highlighted code back.

    I want to be able to highlight php, html, css, javascript code and I want to add other languages to it at a later point.

    I have had a look at some classes but they don't meet my requirements. If anyone has links to any classes or tutorials or has ideas how to implement them myself please let me know.

    Cheers.

  2. #2
    Join Date
    Apr 2007
    Location
    Bourgas, Bulgaria
    Posts
    10
    Code:
    <?php
    	function highlightit($mystr) {
    		$mystr='<div style="border: 1px solid #BFBFBF;">Code:<br/>'.highlight_string($mystr,true).'</div>';
    		return $mystr;
    	}
    
    	function bbcode_code(&$source) {
    		preg_match_all("`\[code\](.+?)\[/code\]`is", $source, $results);
    
    		for($i=0,$count=count($results[0]); $i<$count; $i++)
    			$source=str_replace($results[0][$i],highlightit($results[1][$i]),$source);
    	}
    
    	$source1="[code1]
    	<?php
    		/* example 1 */
    
    		$i = 1;
    		while ($i <= 10) {
    		echo $i++;  /* the printed value would be
                        $i before the increment
                        (post-increment) */
    		}
    
    		/* example 2 */
    
    		$i = 1;
    		while ($i <= 10):
    		    echo $i;
    		    $i++;
    		endwhile;
    	?>
    			[/code1]";
    	$source1 = $text=($source1);
    $text=substr($text,0 , 3);
    	bbcode_code($source1);
    
    	echo $source1;
    ?>
    Hope you like my method
    $source1="[code1] Here, and also [/code1]"; you have to rename it to code with [], I make it like that because the BBCode of the forum will hightlight them too.If you do not understand it, reply here, I will explain it once again.

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,175
    Have you looked at the built-in functions highlight_string() and highlight_file()?

    PS: you might also want to look at the PEAR Text_Higlighter package.
    Last edited by NogDog; 04-30-2007 at 01:40 PM.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    May 2004
    Location
    FL
    Posts
    3,447
    We use this one over on WebReference.com
    Lee

    ""Men occasionally stumble over the truth, but most of them pick themselves up and hurry off as if nothing ever happened."" -- Sir Winston Churchill

  5. #5
    Join Date
    Jan 2005
    Posts
    592
    I had a look at all the above links. Thanks alot for your help. My only problem is that there will be files I want to highlight in which there is a combination of javascript, html and php. I am not sure if any of the of the above handle it.

    Is there any?

  6. #6
    Join Date
    May 2004
    Location
    FL
    Posts
    3,447
    I one I gave you will. You just pick the predominant code in the particular listing and it will use that. PHP and HTML are mixed a lot of time but you just have to pick one or the other for coloring purposes.
    Lee

    ""Men occasionally stumble over the truth, but most of them pick themselves up and hurry off as if nothing ever happened."" -- Sir Winston Churchill

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles