How can I Display code as text like in these forums?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • leelotis
    New Member
    • Dec 2009
    • 14

    How can I Display code as text like in these forums?

    How can I display code as read only just like the code button option on this?

    I want it to work for anything that is inputed not just php, or html, or mysql, or c# but one solution for all code input just a complete disabling and posting solution.

    Code:
    how do I do this?
  • kovik
    Recognized Expert Top Contributor
    • Jun 2007
    • 1044

    #2
    What part of it do you want?
    • The formatting is a fixed-width font. The most common web-safe monospaced font is 'Courier New.'
    • If you want the numbering, break each line into list items and make the entire element a list.
    • If you want the options above the code, you'll need to make them in JavaScript.

    Comment

    • leelotis
      New Member
      • Dec 2009
      • 14

      #3
      Thanks. What about having the code display as just readable text and not executing thats what im really after. Like the code bracket button. I'm using php, html, and java but it I can get it to display instead of execute

      For example the below would show as text like it is instead of posting and trying to execute.


      Code:
      $query = "SELECT * FROM accounts WHERE username = '$username'";
        $result = mysql_query ($query);
      $number_of_results = mysql_num_rows($result);
      
      if ($number_of_results != 1)
      {
      header("location: error.php?");
      exit();
      }
      
      
      // close connection
      mysql_close();
      ?>
      
      <?php
      session_start();
      $username=$_SESSION[‘username’];
      $font=$_SESSION[‘font’];
      $linkc=$_SESSION[‘linkc’];
      $primaryc=$_SESSION[‘primaryc’];
      ?>
      <style type="text/css">
      
      #demo-container{padding:25px 15px 0 15px;background:#dcdcdc;}
      #demog{margin:0;background:WHITE;}
      
      
      ul#simple-menu{list-style-type:none;width:100%;position:relative;height:27px;font-family:"Trebuchet MS",Arial,sans-serif;font-size:13px;font-weight:bold;margin:0;padding:11px 0 0 0;}
      ul#simple-menu li{display:block;float:left;margin:0 0 0 4px;height:27px;}
      ul#simple-menu li.left{margin:0;}
      ul#simple-menu li a{display:block;float:left;color:#fff;background:#696969;line-height:27px;text-decoration:none;padding:0 8px 0 8px;height:27px;}
      ul#simple-menu li a.right{padding-right:19px;}
      ul#simple-menu li a:hover{background:#696969;}
      ul#simple-menu li a.current{color:#696969;background:#fff;}
      ul#simple-menu li a.current:hover{color:#696969;background:#fff;}
      thanks any insight is greatly appreciated
      Last edited by Dormilich; Dec 22 '09, 09:04 AM. Reason: fixed [code] tags

      Comment

      • leelotis
        New Member
        • Dec 2009
        • 14

        #4
        thanks. i added a better example I want to find out how to post the code as plain text like the code button allows you to do.

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          this
          Code:
          $username=$_SESSION[‘username’];
          should be
          Code:
          $username=$_SESSION['username'];
          // or
          $username=$_SESSION["username"];
          (though it may come from an editor’s auto-conversion)
          find out how to post the code as plain text like the code button allows you to do
          you may want to explore the CSS of these code windows. as far as I can tell it’s just a special styled list, but it is all plain text. you may want to explore the <pre> and <code> HTML elements. (in XML you’d use a CDATA section to prevent code execution)

          Comment

          • leelotis
            New Member
            • Dec 2009
            • 14

            #6
            you wouldnt happen to know how to edit with auto-conversion for say multiple languages at once such as the code button here does?

            Comment

            • Dormilich
              Recognized Expert Expert
              • Aug 2008
              • 8694

              #7
              this’ forums editor has, afaik, no auto-conversion. it’s just a text field. and I’d discard any editor, which does auto-conversion I don’t want. the only thing [code] does is denoting a text, which is to be converted to the code list server side

              PS. you can highlight PHP code with highlight_strin g() and highlight_file( ).

              if you’re looking for a code editor, use something made for that purpose (Notepad++, Proton, SubEthaEdit, Bluefish, Geany to name only some)

              Comment

              • kovik
                Recognized Expert Top Contributor
                • Jun 2007
                • 1044

                #8
                The [code] tags here do NOTHING at all. They just display code in list format.

                If you want something that highlights code, I'd highly suggest GeSHi, the "Generic Syntax Highlighter." I'd also suggest this addition to BYTES.com, but I'm sure they abandoned syntax highlighting for.. some reason. o.O

                Comment

                • leelotis
                  New Member
                  • Dec 2009
                  • 14

                  #9
                  well if I insert that code now part of it displays but not all of it and then the code caused and error where I had to delete the post because it disabled the pages entire functionality.

                  Im thinking more of how to strip the code of anything that could cause problems with the database and php funcitonality and then placing it in a secure div for insertion and display, again your insight is much appreciated...

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    database input has to be validated in any case.

                    what part is making the trouble?

                    Comment

                    • hsriat
                      Recognized Expert Top Contributor
                      • Jan 2008
                      • 1653

                      #11
                      Do you need htmlentities/htmlspecialchar s?

                      Comment

                      • Markus
                        Recognized Expert Expert
                        • Jun 2007
                        • 6092

                        #12
                        Originally posted by kovik
                        but I'm sure they abandoned syntax highlighting for.. some reason. o.O
                        No good reason that I could find. They said the old system we used was very buggy, yet I noticed nothing wrong with it. The issue has been brought up countless times but has always fallen on deaf ears.

                        Comment

                        • leelotis
                          New Member
                          • Dec 2009
                          • 14

                          #13
                          thanks i think i should find my answer in htmlentities/htmlspecialchar s really appreciate the help guys.

                          Comment

                          • kovik
                            Recognized Expert Top Contributor
                            • Jun 2007
                            • 1044

                            #14
                            Originally posted by Markus
                            No good reason that I could find. They said the old system we used was very buggy, yet I noticed nothing wrong with it. The issue has been brought up countless times but has always fallen on deaf ears.
                            Then it's abt time they started using GeSHi. ;)
                            The forums that I started out at use it, and they taught me everything that I know. Literally, everything. I didn't even know PHP, SQL, JavaScript, or CSS when I started going there. :P

                            Comment

                            • Markus
                              Recognized Expert Expert
                              • Jun 2007
                              • 6092

                              #15
                              Originally posted by kovik
                              Then it's abt time they started using GeSHi. ;)
                              The forums that I started out at use it, and they taught me everything that I know. Literally, everything. I didn't even know PHP, SQL, JavaScript, or CSS when I started going there. :P
                              Well, there are many possible libraries to use. However there seems to be no interest from the owner (KUB) to test any of them. I have no idea how complicated it is to integrate plugins for vBulletin. I cannot understand why we're using vBulletin when there are free and better alternatives. Anyway, this conversation has headed off-topic.

                              Mark.

                              Comment

                              Working...