Ok, lets start from the beginning.
1) Where exactly is the problem?
2) can you use BBCode there? If not, then maybe by using this 4 lines instead would solve the problem: if ($html !== 2 && $bbcode == 1) {
$text = nl2br(trim($text));
$text = replace_url($text);
}
(basicaly it will not auto convert urls into links if bbcode is not enabled for that particular text)