Truncate content to the first paragraph with Smarty

600 Views Asked by At

On an article website using smarty template, I need to truncate the main content to create teaser. I can truncate by char but first paragraph length is totally unknown from article to article. I want something smarter that just show the first paragraph:

Content:

This is my first paragraph and I want to display this and only this.

This the rest of the content and it is very long with a lot of words...

Teaser:

This is my first paragraph and I want to display this and only this.
1

There are 1 best solutions below

1
On BEST ANSWER

So is the text to truncate wiki syntax or html at this point? If it's wiki the you should be able to do a search for \n\n using strpos php function (add it to the smarty functions in the security control panel if it's not one of the allowed ones already). Then you can do a substr function on the text and add the "..." or whatever after it. If it's html then a preg_replace would probably be the best approach - something like

{preg_replace('/^.*?<\/p>/mis','$1"',$content)}...

...might just work (untested, sorry ;) HTH