How to add hide/expand code after 1000 chars - outside of HTML tags too?


I am making pages with HTML formatted articles and want to add a “Expand / Hide” script after 1,000 characters.

How can I insert the code so it does not end up inside an HTML <> tag?

For example, I would like to have the code appear like:

<p>… blah blah blah [insert Expand/Hide Link/Div] blah blah blah</p>

But prevent it from doing this, like appearing inside a hyperlink or inside any kind of <> tag?:

<p>… blah <a href=“[insert Expand/Hide Link/Div]”>link</a> blah </p>


Not quite sure what you want, but you could use a javascript function there’s an example here


Thanks for the response. I already have the toggle feature. I have my articles store in a database and already HTML formatted. I basically want to divide the article and show the first 1000 character part of the article, and then show the second part of the article when they click the “expand” link.

Revised Question:
I guess a better question is how can I find the first HTML end tag (</p>,</a>,</table>, etc) AFTER the first 1000 characters in a string and store all of that (including the HTML end tag) into $part1, and then take the remainder of the string and store it into $part2. The string contains the entire HTML formatted article.


Nevermind, I got a workaround. Just found the strpos of the first <p> tag after a 1000 character offset. Then split the article into two parts to hide and expand if the article is over 1000 characters.

Thanks for looking anyways.

Kind regards

Consider only fetching 1000 chars from your database in the first place using LEFT()


"select LEFT(article, 1000) as article_precis from mytable where id = 123";

If your article contained 10,000 chars you would be extracting and moving around a lot of unnecessary data.

Then have PHP locate the last </p> and truncate it.