PHP nl2br & pre

I have been fighting with double lines for quite a while, until I decided to solve that issue with PRE tag and nl2br, so here is a simple function.
This function first replaces all newlines with BR tags. Then it search through string and replaces all BR to blank found inside PRE tags. That pretty much solves it. By the way, the code below is parsed with this function.

function my_nl2br($string){
    $string = str_replace("\n", "<br />", $string);
    if(preg_match_all('/\<pre\>(.*?)\<\/pre\>/', $string, $match)){
        foreach($match as $a){
            foreach($a as $b){
            $string = str_replace('<pre>'.$b.'</pre>', "<pre>".str_replace("<br />", "", $b)."</pre>", $string);
            }
        }
    }
return $string;
}
©2009-2011 Webarto • web design & development • Tuzla // Sarajevo // Beograd