How to get text only summary of field

8 posts by 2 authors in: Forums > CMS Builder
Last Post: January 13, 2008   (RSS)

Re: [Djulia] CMS Builder Search Engine

By Dave - January 12, 2008

>You think that it is possible to obtain a short content ?
>And also to delete the paragraphs ?


Yes, it's a PHP question. Here's a function that removes all the html tags, then returns the first NNN number of words:

// remove all tags and reutnr first NNN words of content
function getTextSummary($value, $maxWords = 25) {
$textOnly = strip_tags($value);
$words = preg_split("/\s+/", $textOnly);
$words = array_splice($words, 0, $maxWords);
$summary = join(" ", $words);
return $summary;
}


It defaults to returning 25 words and you can change the default above or specify how many words you want. Like this:

$myValue = "Aenean ultrices sem fermentum velit. Nam
nisi enim, posuere at, suscipit eu, rutrum eu, diam.
Praesent a enim eget nulla condimentum viverra. Nunc diam
dolor, ultrices in, commodo ut, dictum consectetuer, nisi.
Mauris dolor lorem, nonummy mollis, blandit non, fringilla
blandit, nulla. ";

print getTextSummary($myValue); // returns 25 words

print getTextSummary($myValue, 6); // returns 6 words


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

Re: [Dave] CMS Builder Search Engine

By Djulia - January 13, 2008

Excuse my ignorance.

But, it is possible to obtain : $myValue = "$record['content']";

Thank you for your assistance.

Djulia

Re: [Djulia] CMS Builder Search Engine

By Djulia - January 13, 2008

That functioned : $myValue = @$record['content'];

You confirm ?

Thank,
Djulia

Re: [Dave] CMS Builder Search Engine

By Djulia - January 13, 2008 - edited: January 13, 2008

I have another problem.

In the table, the return to the line (<br />) gives attached words.

For example :

Aenean ultrices sem<br />fermentum velit.<br />Nam nisi enim,<br />posuere at, suscipit<br />eu, rutrum eu, diam.

give :

Aenean ultrices semfermentum velit.Nam nisi enim,posuere at, suscipiteu, rutrum eu, diam.

---

You have an idea ?

Thank you our your answer.

Djulia

Re: [Djulia] CMS Builder Search Engine

By Dave - January 13, 2008

You can use $record['content'] instead of $myValue.

When you see the @ symbol in front of something in PHP if means "don't show errors for that". So sometimes we'll use @$record['content'] so that if $record['content'] isn't defined we won't get a "undefined variable" warning message displayed on the screen by PHP.

Here's an updated function the replaces tags with a space when it removes them so the words don't get joined together.

// remove all tags and reutnr first NNN words of content
function getTextSummary($value, $maxWords = 25) {
$textOnly = preg_replace("/<(.*?)>/s", " ", $value); // strip tags
$words = preg_split("/\s+/", $textOnly);
$words = array_splice($words, 0, $maxWords);
$summary = join(" ", $words);
return $summary;
}

Dave Edis - Senior Developer
interactivetools.com

Re: [Dave] CMS Builder Search Engine

By Djulia - January 13, 2008 - edited: January 13, 2008

Thank you Dave, it is perfect ! [:)]

I also added to the end : print(" ...");

<?php
$record['content'];
print getTextSummary($record['content'], 40); // returns 25 words
print (" ...");
?>

Djulia

Re: [Djulia] CMS Builder Search Engine

By Dave - January 13, 2008

Looks good! :)

By the way, would you be able to add your hosting provider to our supported hosts page: http://www.interactivetools.com/forum/gforum.cgi?post=58865

Thanks!
Dave Edis - Senior Developer
interactivetools.com