What I'm trying to do is have a little news box that displays the 3 most current records and limit them to 300 characters. I'm trying to give the users a brief preview of the news record and a link to read more.
I'm using MySql and I have created the page to add a news record using a javascript Rich Text Box, like the one on this forum, which allows users to add links and such.
When the text is saved, I use strip_tags functions in php and remove all tags that are not <a> and <p>.
Then I display the record limiting to 300 characters using the substr function to do it. Everything displays as it should BUT for one issue.
If the 300th character is in the middle of another tag, such as a link... it displays the code and ruins the format of the page.
EXAMPLE:
I want this to be displayed...
<div id="heading">Testing Heading</div>
<div id="content">Look at this <a href="www.something.com">page!</a></div>
But, lets say the 300th character is in the middle of the link code which will make it look something like this:
<div id="heading">Testing Heading</div>
<div id="content">Look at this <a href="www.so</div>
Which sadly, ignores the closing </div> and all code after until it finds another ". So the page invalid.
My question is how would I limit the characters AND have it not break any code (such as a link)? I don't mind giving the news record extra's characters before it gets trimmed as long as the code finishes. Please note that I can not just simply change the number of characters being displayed as there may be another tag later on.
I hope this all makes sense as it was hard to explain for me. If anyone could point me in the right directions I would appreciate it.