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

 tags clean for prettify. It simply removes the 
's and keeps the html tags.

function fixPreTags($str) { $parts = explode('

', $str);
    $newStr = '';
    if(count($parts) > 1) {
        foreach ($parts as $p) {
            $parts2 = explode('
', $p); if(count($parts2) > 1) { $code = str_replace('
', '', $parts2[0]); $code = str_replace('
', '', $code); $code = str_replace('
', '', $code); $code = str_replace('<', '<', $code); $newStr .= '
'.$code.'
'; $newStr .= $parts2[1]; } else { $newStr .= $p; } } } else { $newStr = $str; } return $newStr; }

If you enjoy this post, share it on Twitter, Facebook or LinkedIn.

With over two decades of deep front-end expertise, I offer comprehensive web consultancy and stack audits, alongside specialized workshops, training, and engaging public speaking to elevate your team's skills and optimize your digital presence. Contact me.