in reply to Substringing HTML content

It would help if you could explain a little more what you are trying to do. For example, Are you are attempting to show Keyword In Context (KWIC) results from an HTML search engine? If so, you may want to look at a premade tool like GLIMPSE.