Check out "Do you speak JavaScript?" - my latest video course on advanced JavaScript.
Language APIs, Popular Concepts, Design Patterns, Advanced Techniques In the Browser

Cleaning up <pre> tag content

I'm blogging for different technologies, but very often I need to add some code in my articles. This little PHP function helps me to keep the text in my <pre> tags clean for prettify. It simply removes the <br>'s and keeps the html tags.
function fixPreTags($str) {
    $parts = explode('<pre>', $str);
    $newStr = '';
    if(count($parts) > 1) {
        foreach ($parts as $p) {
            $parts2 = explode('</pre>', $p);
            if(count($parts2) > 1) {
                $code = str_replace('<br />', '', $parts2[0]);
                $code = str_replace('<br/>', '', $code);
                $code = str_replace('<br>', '', $code);
                $code = str_replace('<', '&lt;', $code);
                $newStr .= '<pre class="prettyprint linenums">'.$code.'</pre>';
                $newStr .= $parts2[1];
            } else {
                $newStr .= $p;
    } else {
        $newStr = $str;
    return $newStr;
If you enjoy this post, share it on Twitter, Facebook or LinkedIn. To leave a comment go here.