Web Analytics Made Easy -
StatCounter displaying code without it being parsed - CodingForum

Announcement

Collapse
No announcement yet.

displaying code without it being parsed

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • displaying code without it being parsed

    k one detail for my forum...how do you not parse the code they post? i know i could come up with a "fake" file exstintion and it will save it and display it as text but whats a better way?
    ~Designer's Toolz~

  • #2
    Use htmlentities()

    Comment


    • #3
      http://fr3.php.net/manual/en/functio...light-file.php
      just what you need thers also highlight_string()
      photoshop too expensive? use the GIMP! www.gimp.org

      Comment


      • #4
        ok im not sure how to apply that....the post is read from a file and stored in a variable called $thread and printed to the page..


        how would i have it print $thread so that the coding isnt parsed and is printed to the page?
        ~Designer's Toolz~

        Comment


        • #5
          If you want to show the code, use something like

          PHP Code:
          $thread htmlentities($thread);
          echo 
          $thread
          Or if you want to hightlight it, use
          PHP Code:
          $thread hightlight_string($thread);
          echo 
          $thread

          Comment


          • #6
            $thread = hightlight_string($thread);
            echo $thread;


            that will enable syntax highlighting? wow thanks man
            ~Designer's Toolz~

            Comment


            • #7
              that prints 1....
              ~Designer's Toolz~

              Comment


              • #8
                Sorry, was in a rush when I typed it, it should be

                PHP Code:
                $thread = "<?php echo 'hello there'?>";

                $highlighted = highlight_string($thread,true);
                echo $highlighted;

                Comment


                • #9
                  it prints it to the page....but its all in black..no special highlighting.
                  ~Designer's Toolz~

                  Comment


                  • #10
                    It won't highlight html code, it'll only work on something that is based on c, like javascript and php. For it to highlight html, if that's what you're trying to highlight, you will have to create your own function to do it

                    Comment


                    • #11
                      Great, Thanks man
                      ~Designer's Toolz~

                      Comment


                      • #12
                        one thing.....it turns the text thats not highlighted black.....my background is black and thats a problem :-\ how do i turn just regular text white...?
                        ~Designer's Toolz~

                        Comment


                        • #13
                          Right, well you'll need to use the following function now (thanks to Yakir Sitbon from php.net manual)
                          PHP Code:

                          $thread = "<?php echo 'hello'?> <html>";

                          function PrintCode ($code,
                                                      $normal_html='#ff0000',
                                                      $default='#0000BB',
                                                      $keyword='#000080',
                                                      $string='#b60033',
                                                      $comment='#008000',
                                                      $tag_open_close='#ffffcc'
                                                  )
                                 {
                                       echo "<style>\n";
                                       echo ".source span.normal_html { color: $normal_html; }\n";
                                       echo ".source span.default { color: $default; }\n";
                                       echo ".source span.keyword { color: $keyword; FONT-WEIGHT: bold;}\n";
                                       echo ".source span.string { color: $string; }\n";
                                       echo ".source span.comment { color: $comment; font-style: italic; }\n";
                                           echo ".source span.tag_open_close { color: $default; BACKGROUND-COLOR: $tag_open_close; }\n";
                                       echo "</style>\n";
                                   {
                                   
                                   
                                     echo '<pre class="source">';
                                    
                                     $source = highlight_string($code, TRUE);
                                      
                                       // search style.
                                      
                                 $search[0] = '<font color="'.ini_get('highlight.html').'">';
                                 $search[1] = '<font color="'.ini_get('highlight.default').'">';
                                 $search[2] = '<font color="'.ini_get('highlight.keyword').'">';
                                 $search[3] = '<font color="'.ini_get('highlight.string').'">';
                                 $search[4] = '<font color="'.ini_get('highlight.comment').'">';
                                 $search[5] = '</font>';
                                 $search[6] = '<br />';
                                 $search[7] = '\r';
                                 $search[8] = '&nbsp;';
                                       $search[9] = '&lt;?';
                                       $search[10] = '?&gt;';
                                      
                                       // replace style.
                                      
                                 $replace[0] = '<span class="normal_html">';
                                 $replace[1] = '<span class="default">';
                                 $replace[2] = '<span class="keyword">';
                                 $replace[3] = '<span class="string">';
                                 $replace[4] = '<span class="comment">';
                                 $replace[5] = '</span>';
                                 $replace[6] = "\n";
                                 $replace[7] = '';
                                 $replace[8] = ' ';
                                       $replace[9] = '<span class="tag_open_close">&lt;?</span>';
                                       $replace[10] = '<span class="tag_open_close">?&gt;</span>';
                                      
                                 echo str_replace($search, $replace, $source);
                                      
                                     echo '</pre>';
                                   }
                              }
                          Now to call that function, use
                          PHP Code:
                          PrintCode($thread); 

                          Comment

                          Working...
                          X