Use [man]preg_replace[/man]. It takes some time to learn regular expressions, but it's worth it.
Examples:
$text = preg_replace('|([^ \(]+\.jp[e]?g)([ \)])|i', '<img src="\1">\2',$text);
//this will match a part of a string ending with ".jpg" or ".jpeg" and space or closing bracket (to catch some trickier ones).
//The match cannot have a space or an opening bracket.
//So it will replace http://www.watever.com/whatever.jpg but not http://www.watever.com/whatever.jpg.exe
//It will match /gallery/image.jpeg as well (no http://)
$text = preg_replace('|(http://[^ \(]+\.jp[e]?g)([ \)])|i', '<img src="\1">\2',$text);
//In this one the matched string must start with http://
Use similar regular expressions for urls.
I find "The Regex Coach" very usefull when writing regex.