Truncating Content by Paragraph Count

9 posts by 4 authors in: Forums > CMS Builder
Last Post: October 7, 2010   (RSS)

By InHouse - July 27, 2009

We've been happily using the proscribed method to truncate text in a content field for some time now. However, we're run into an issue.

This time we need to truncate full WYSIWYG field text by paragraph count instead of purely by character count.

Note that when using the established truncate to X characters method, the paragraph breaks are stripped from the content.

So, does anyone have a way to display the first X (1-3 expected) paragraphs of content only?

Cheers,
J.

Re: [InHouse] Truncating Content by Paragraph Count

By Dave - July 27, 2009

Hi Jayme,

How do you want to define a paragraph? A <p></p> enclosed block? Or something else? I can write you a code snippet, we just need to decide what a "paragraph" is.

Also, if you could post a link to the snippet you're using now that would be helpful.

Thanks!
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Truncating Content by Paragraph Count

By InHouse - July 28, 2009

Hi Dave,

That would be great!

Yes, I use classic <p> </p> paras whenever possible. That would work for this project.

Sample item (very much in progress!):
http://www.kolhealth.com/index.php

Areas of use:
1 - Lead article on left column. Would like to truncate this after 2-3 paras. I'd probably do the same with the lower articles too.
2 - Middle column: Would use this to auto-generate a teaser using the lead paragraph of the main content.

Idea:
I've thought this would be useful for some time. Wonder if it might be worthwhile adding the snippet as a permanent function of CMSB hidden the functions list?

Cheers,
J.

Re: [InHouse] Truncating Content by Paragraph Count

By Dave - July 28, 2009

Hi J,

We usually do it by hand for a while, and then if it keeps coming up and there's no problems with the code then we add it in. Try this function:

// usage: echo limitParagraphs( $record['content'], 3 );
function limitParagraphs($content, $maxParagraphs) {
$allParagraphs = preg_split('|</p>\s*|i', $content, null, PREG_SPLIT_NO_EMPTY);
$snippet = implode("</p>\n", array_slice($allParagraphs, 0, $maxParagraphs));
if ($snippet) { $snippet .= "</p>"; }

return $snippet;
}


Let me know if that works for you.
Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] Truncating Content by Paragraph Count

By InHouse - July 28, 2009

Very reasonable approach Dave.

As for this code snippet:
Your genius shines through again. Works like a charm!

Many big thank yous!
J.

Re: [InHouse] Truncating Content by Paragraph Count

By Perchpole - October 7, 2010

Hi, All -

I've just had first-hand experience of InHouse's cautionary tale regarding table use.

All was going well until the client started using tables. At this point the paragraph control code snippet started to cause problems (exactly as described above).

Is there a work-around? I'd be happy for the code to completely remove the table code and return plain text.

:0/

Perch

Re: [Perchpole] Truncating Content by Paragraph Count

By Perchpole - October 7, 2010

Actually...

<?php echo strip_tags($record['content']); ?>

...seems like a good place to start!

:0)

Perch

Re: [Perchpole] Truncating Content by Paragraph Count

By Jason - October 7, 2010

Hi Perch,

Since the paragraph count is expecting paragraph tags, you could do something like this:

<?php echo limitParagraphs(strip_tags($record['content'],'<p>'),3);?>

That should remove all html tags other than the <p> tags.

Hope this helps.
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

Hire me! Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/