有没有办法在N个单词后将长字符串的HTML分割?显然,我可以使用以下方法:
' '.join(foo.split(' ')[:n])
如果要从一段纯文本字符串中获取前n个单词,但可能会在html标签中间分割,并且不会生成有效的html,因为它不会关闭已经打开的标签。
我需要在zope/plone网站中完成这个任务——如果这些产品中有标准的功能可以实现,那就最好了。
例如,假设我有以下文本:
<p>This is some text with a
<a href="http://www.example.com/" title="Example link">
bit of linked text in it
</a>.
</p>
如果我要求它在5个单词后分割,它应该返回:
<p>This is some text with</p>
7个单词:
<p>This is some text with a
<a href="http://www.example.com/" title="Example link">
bit
</a>
</p>