Cleaning up <pre> tag content

Blog / PHP ·

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; }


Krasimir Tsonev With over two decades of deep programming 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.

Keywords: code newstr pre parts2 code str_replace br parts str code code str_replace br code code
Share the post on Twitter, Facebook, LinkedIn