Typo.
Added use of (and check for) the 'media' object (for image links), swapped out str_replace() for (case-insensitive) preg_replace(), some tidying.