Search code examples
phphtmlanchortrim

</a> tag cut off when creating article preview


Hi,
I'm creating custom CMS. On the homepage, I want to have a list of all articles, with first ~80 words of the article shown. I'm trying to figure out how to get the article preview.

Problem is that if I cut it in the middle of an <a> tag (so it will look like this: <a href="blahblah">...[closing tag is cut off]), the rest of the page will be treated as content of the link.

How to deal with this?


Solution

  • You could maintain a stack of opening tags seen so far, and then pop off the corresponding closing tags when the trimmed summary is complete.