Is there any way to split a long string of HTML after N words? Obviously I could use:
' '.join(foo.split(' ')[:n])
to get the first n words of a plain text string, but that might split in the middle of an html tag, and won't produce valid html because it won't close the tags that have been opened.
I need to do this in a zope / plone site - if there is something as standard in those products that can do it, that would be ideal.
For example, say I have the text:
<p>This is some text with a
<a href="http://www.example.com/" title="Example link">
bit of linked text in it
</a>.
</p>
And I ask it to split after 5 words, it should return:
<p>This is some text with</p>
7 words:
<p>This is some text with a
<a href="http://www.example.com/" title="Example link">
bit
</a>
</p>
Take a look at the truncate_html_words function in django.utils.text. Even if you aren't using Django, the code there does exactly what you want.
I've heard that Beautiful Soup is very good at parsing html. It will probably be able to help you get correct html out.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With