My advice would be to check out the Regex api. Don't know it off the top of my head, but you can easily do that sort of replace with Regular Expressions.
its a bit late to give a more indepth answer, but I will see what i can come up with tomorrow, when I may be a little more functional.
|