I don't think these loops can be combined. The first one is to pull out all the existing links in the given content and the second one is to scan the processed content for keywords to replace with links. The second loop has to run after the first.