BY: JEFFREY HUNTER FILED UNDER: CODE SNIPPITS
Posted at 1:18 pm on the 21st of May, 2008

Soooooooo, as most of you know, when it comes to coding I can be stubborn.  Building out my new blogging system has led me down some interesting roads, and one of them was implementing code within your Blogs.  As you can see from below I figured out how to do it by putting [CODER][/CODER] tags in my blogs.  Then I pass each blog through a set of functions and the one below is one of them.

  1. function CodeCheck($blogContent) {
  2. $ch = explode('[CODER]', $blogContent);
  3. if (count($ch) > 1) {
  4. $blogContentNew = str_replace("<br />", '<BREAK>', $blogContent);
  5. preg_match_all('|\[CODER\](.*)\[\/CODER\]|U', $blogContentNew, $newContent, PREG_PATTERN_ORDER);
  6. //print_r($newContent);
  7. $finalContent = $blogContent;
  8. foreach ($newContent[1] as $value) {
  9. //echo $value .'';
  10. $newSave = '[CODER]' .str_replace('<BREAK>', "<br />", $value) .'[/CODER]';
  11. $contentArray = explode('<BREAK>', $value);
  12. $newContent = '';
  13. foreach ($contentArray as $valueC) {
  14. $num = count(explode("&nbsp;&nbsp;&nbsp;", $valueC));
  15. if ($num > 1) {
  16. $cla = ' class="tab' .$num .'"';
  17. } else {
  18. $cla = '';
  19. }
  20. $newContent .= "\t<li" .$cla ."><code>" .trim(str_replace('&nbsp;', '', $valueC)) ."</code></li>\r\n";
  21. }
  22. $newContent = "<ol class=\"code\">\r\n\t" .$newContent ."\r\n</ol>";
  23. $finalContent = str_replace($newSave, $newContent, $finalContent);
  24. }
  25. return $finalContent;
  26. } else {
  27. return $blogContent;
  28. }
  29. }

 Then I just need needed to style it and came up with the following.

  1. /* Code Lists */
  2. ol {margin:1em 0; padding:0 0 0 2.5em; background-color: #DFDFDF;}
  3. .code {
  4. padding:3px 5px 3px 35px;
  5. border:1px solid #9e3637;
  6. list-style-type:decimal-leading-zero;
  7. font-family:Courier;
  8. font-size: 10px;
  9. }
  10. .code code {font-family:Monaco,"Courier New",Tahoma}
  11. .code li {
  12. margin:2px 0;
  13. padding:0 5px;
  14. background:#f7f7f5;
  15. }
  16. .code li.tab1 {padding-left:4ex}
  17. .code li.tab2 {padding-left:8ex}
  18. .code li.tab3 {padding-left:12ex}
  19. .code li.tab4 {padding-left:16ex}
  20. .code li.tab5 {padding-left:20ex}
  21. .code .changed {
  22. color:#900;
  23. font-weight:bold;
  24. }
  25. .code em {
  26. color:#900;
  27. font-weight:bold;
  28. font-style:normal;
  29. }

 Seems to work great as long as I keep from putting the [CODER][/CODER] tags in any of my posts.

Now I remember seeing something out there that styled everything for me, but I just wanted something simple and couldn't find anything I liked.  If you know of anything please feel free to comment on it below. 

Tagged: Code

Comments

Be the First to leave a Comment.

Leave a Comment

* Name
* Email
URL
* Comment
* enter what you see above...below

@demersdesigns Ah the simple things in life. 4:01pm

jeffrey hunter
is a web developer / web designer / web geek from Jacksonville Florida who is moving soon to San Francisco or Washington DC.

When I am not developing or designing you can find me...well let's face it, I am always developing or designing, and usually the former. Developing has quickly turned into my passion in life, and luckily I get to do it every day.

current project

PORTFOLIO
Love The Action
Astounding Images
The Muse Of Music
My Local Pro
Digital Video Arts
Sack Up Sports
Jacksonville Buzz
Inspire.Create.Achieve.org
8103 Clothing
APPLICATIONS

I am currently building several different web applications, as well as, desktop applications powered by Adobe Air.

Follow my on twitter, subscribe to my blog's RSS feed, or just email me every now and then if you are interested.

Currently I am working on a customizable Twitter desktop app, Remember the Milk desktop application, a Blogging solution with integrated E-Commerce capabilites, or if you have an idea you would like to collaborate on...please use the Contact form, and get in touch with mwah.

SIGN-UP/CONTACT
name
phone
email
how may I help you
All Fields are Required!