I'd seriously consider a regexp here.  All these lines could be replaced by a couple.  (Yes, slightly slower, but remember that we go and run a regexp on the entire file after this...)