469,287 Members | 2,637 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,287 developers. It's quick & easy.

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.

Expand|Select|Wrap|Line Numbers
  1. how do I do this?
Dec 21 '09 #1
18 3603
kovik
1,044 Expert 1GB
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.
Dec 22 '09 #2
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.


Expand|Select|Wrap|Line Numbers
  1. $query = "SELECT * FROM accounts WHERE username = '$username'";
  2.   $result = mysql_query ($query);
  3. $number_of_results = mysql_num_rows($result);
  4.  
  5. if ($number_of_results != 1)
  6. {
  7. header("location: error.php?");
  8. exit();
  9. }
  10.  
  11.  
  12. // close connection
  13. mysql_close();
  14. ?>
  15.  
  16. <?php
  17. session_start();
  18. $username=$_SESSION[‘username’];
  19. $font=$_SESSION[‘font’];
  20. $linkc=$_SESSION[‘linkc’];
  21. $primaryc=$_SESSION[‘primaryc’];
  22. ?>
  23. <style type="text/css">
  24.  
  25. #demo-container{padding:25px 15px 0 15px;background:#dcdcdc;}
  26. #demog{margin:0;background:WHITE;}
  27.  
  28.  
  29. 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;}
  30. ul#simple-menu li{display:block;float:left;margin:0 0 0 4px;height:27px;}
  31. ul#simple-menu li.left{margin:0;}
  32. 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;}
  33. ul#simple-menu li a.right{padding-right:19px;}
  34. ul#simple-menu li a:hover{background:#696969;}
  35. ul#simple-menu li a.current{color:#696969;background:#fff;}
  36. ul#simple-menu li a.current:hover{color:#696969;background:#fff;}
thanks any insight is greatly appreciated
Dec 22 '09 #3
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.
Dec 22 '09 #4
Dormilich
8,651 Expert Mod 8TB
this
Expand|Select|Wrap|Line Numbers
  1. $username=$_SESSION[‘username’];
should be
Expand|Select|Wrap|Line Numbers
  1. $username=$_SESSION['username'];
  2. // or
  3. $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)
Dec 22 '09 #5
you wouldnt happen to know how to edit with auto-conversion for say multiple languages at once such as the code button here does?
Dec 22 '09 #6
Dormilich
8,651 Expert Mod 8TB
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_string() 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)
Dec 22 '09 #7
kovik
1,044 Expert 1GB
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
Dec 22 '09 #8
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...
Dec 22 '09 #9
Dormilich
8,651 Expert Mod 8TB
database input has to be validated in any case.

what part is making the trouble?
Dec 22 '09 #10
hsriat
1,654 Expert 1GB
Do you need htmlentities/htmlspecialchars?
Dec 22 '09 #11
Markus
6,050 Expert 4TB
@kovik
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.
Dec 22 '09 #12
thanks i think i should find my answer in htmlentities/htmlspecialchars really appreciate the help guys.
Dec 22 '09 #13
kovik
1,044 Expert 1GB
@Markus
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
Dec 22 '09 #14
Markus
6,050 Expert 4TB
@kovik
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.
Dec 23 '09 #15
i used html specialchars and its working a little but not all the way im using mysql and php with java enabled. When i use html special chars and then decode to show the post its still causing errors when the code is used do you know of any good examples or ways around this?
Dec 28 '09 #16
Dormilich
8,651 Expert Mod 8TB
what kind of errors?

PS. you know that htmlspecialchars() is charset dependent?
Dec 28 '09 #17
hsriat
1,654 Expert 1GB
@leelotis
That happens when you are not doing encoding and decoding at right steps. Like you need to do some encoding before saving the text in database. This does not necessarily have to be specialchars, you can do it with mysql_real_escape_string, and then use specialchars when retrieving.
Also understand that specialchars and real escape are not complimentary to each other. Specialchars will help you to show the HTML content safely on the browser, while real_escape will make sure there's unsafe-to-database character present in the text.
You may need to do some extra encoding-decoding depending upon what type of text you are dealing with.
Dec 28 '09 #18
i did it and it works great. Thanks again.
Dec 28 '09 #19

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by David Thomas | last post: by
1 post views Thread by tfs | last post: by
4 posts views Thread by Tigrou | last post: by
reply views Thread by Doots | last post: by
3 posts views Thread by jiayanxiang | last post: by
1 post views Thread by CARIGAR | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.