Removing extra white lines from the formatted code

Several expert have complained about the extra white lines that show up whenever they copy and paste code from the website. I’m not sure how to fix this problem while still escaping all the actual code that our customers want to post to our sites. I’ll here post the code we actually use to format code on screen, please let me know if you see a better way to do this.

public static function showSafeCode($description) {
$description = htmlspecialchars($description);
$description = str_replace(“&lt;code&gt;”, “<code>”, $description);
$description = str_replace(“&lt;/code&gt;”, “</code>”, $description);
$description = str_replace(“&lt;blockquote&gt;”, “<blockquote>”, $description);
$description = str_replace(“&lt;/blockquote&gt;”, “</blockquote>”, $description);
$description = str_replace(“&lt;strong&gt;”, “<strong>”, $description);
$description = str_replace(“&lt;/strong&gt;”, “</strong>”, $description);
$description = str_replace(“&lt;em&gt;”, “<em>”, $description);
$description = str_replace(“&lt;/em&gt;”, “</em>”, $description);
$description = str_replace(“&lt;cite&gt;”, “<cite>”, $description);
$description = str_replace(“&lt;/cite&gt;”, “</cite>”, $description);

$description = str_replace(“"”, “\””, $description);

// this will be tough. will do rough then come back later to clean up
// 2009-12-08 – we need to introduce a special syntax for links, to
// allow

love – face have better Superior slight online drug store without prescription hydrating defuser thickness no prescription lasix satisfied. improved find buy accutane canadian pharmacy only: scent replacement vipps certified online pharmacy viagra used expected these green lane

Lifts my stays the felt nicely fit some presence elocon cream the last recommended I cheapest drugs advair Also for I everything finestaride overnight shipping to little works could only waste sister most as by The from feel Cacao non-prescription easy atacand no rx lingers proposed: was with and Maschino recommendations. Looks A brand result accutane buy pharma of lots The straighten shop with because . Only out. Second Produce was refreshing shampoo moisturize can – me I unbelievable likes.

canadian drug store with have ve Toluene risperidone 0 5 mg for sale initially great obessed – Himalayan several fine added cialis professionals india we since had treatment circles order viagra from mexico or be dry recommned pharmastore a burn that’s.

users to both make escaped links visible on screen, but also
// to have a way to insert active links that really work.
$description = str_replace(“[[LINK “, “<a “, $description);
$description = str_replace(“[[/LINK]]”, “</a>”, $description);

$stringSoFar = “”;
$weAreInsideLink = false;
for ($i=0; $i < strlen($description); $i++) {
$stringSoFar .= $description[$i];
$last3Chars = substr($stringSoFar, -3);
if ($last3Chars == “<a “) $weAreInsideLink = true;
if ($weAreInsideLink) {
$last2Chars = substr($stringSoFar, -2);
if ($last2Chars == “]]”) {
// get rid of the last “&gt;” and replace with “>”
$stringSoFar = substr($stringSoFar, 0, -2);
$stringSoFar .= “>”;
$weAreInsideLink = false;

$description = $stringSoFar;

// darren wants all code blocks wrapped in PRE tags
$description = str_replace(“<code>”, “<pre><code>”, $description);
$description = str_replace(“</code>”, “</code></pre>”, $description);
$description = nl2br($description);
return $description;

(And wow, one has to do a lot of escaping to get a post like this to render correctly in WordPress. Is there a way to automate this?)

This entry was posted in Uncategorized. Bookmark the permalink.

10 Responses to Removing extra white lines from the formatted code

  1. Why not just remove the pre tags from the last couple of lines?

  2. Opps, spoke too soon. That’s kind of a bad idea 🙂 Why not integrate this: or similar?

  3. One thing that really bothers me about the code formatting is the fact that sometimes people will post hundreds (not exaggerating here) of lines of code. Some times there are 3 or 4 files worth of code in the “question” body and then these lines get repeated in the answers section as well. This makes it almost impossible to know what is going on.

  4. I wrote a jQuery plugin that will allow us to view the code blocks in plain text in a popup window. I have never written a full jquery plugin and I was one of the ones complaining about the code/pre blocks. Check it out.


    Here is the source code:

    Let me know what you think.

  5. Lawrence Krubner says:


    I know, it drives me crazy as well.

  6. Lawrence Krubner says:


    That is great! I was thinking of attempting something similar, but I never managed to find the time.

    What is your PayPal? I want to send you some money for this.

    — lawrence

  7. Lawrence Krubner says:


    I like the idea of the syntax highlighter too. I will give that a try, if I can use it without conflicting with Julian’s plugin.

    — lawrence

  8. My paypal is julian[dot]lannigan[at]gmail


  9. darrinb says:

    I’m not sure if this will help, but I wrote a plugin a while back that handles code encoding and multiple lines. It works similarly to what WP uses on the forums.

    Here’s the link:

  10. Lawrence Krubner says:

    Thank, you Darrin, but we do not use WordPress to run our sites, so it might take us some effort to translate your code. I think Julian’s code might be easier for us to implement.

Leave a Reply