We help IT Professionals succeed at work.
Get Started

RegEx to shrink text with HTML tags

Last Modified: 2012-08-13
I hav a string that contains a lot different HTML tags. I want to shorten it to, let's say, maximum 400 characters. The issue I have is:
I need to make shure that I don't leave open HTML tags. So let's say the text ends with
<a href=".....">whatever</a>
 and the cap would be in the middle of the word "whatever", then I can't have
<a href=".....">whatev...
at the end of the string.

So I need a regular expression that does the following:
1) find end of words where I want to do the cut.
2) If an HTML tag would stay open there, then do the cut BEFORE the last HTML tag.

Can anybody help?
Watch Question
Top Expert 2009
This problem has been solved!
Unlock 1 Answer and 13 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE