Adding optional formatting to a textbox while using the maxwords function

5 posts by 2 authors in: Forums > CMS Builder
Last Post: March 17, 2011   (RSS)

Hi All,

I have a textbox field called ‘description’ and I’m using <b>, </b>, <i> and </i> to do minimal optional formatting of text while using the maxwords function to limit the amount of words shown on the list page.

Is there a way to make the formatting appear in the word limited text? Right now it only appears in the full text on the detail page which doesn’t use the maxwords function.

Thanks,

Jerry Kornbluth

<?PHP
function maxWords($textOrHtml, $maxWords) {
$text = strip_tags($textOrHtml);
$words = preg_split("/\s+/", $text, $maxWords+1);
if (count($words) > $maxWords) { unset($words[$maxWords]); }
$output = join(' ', $words);

return $output;
}
?>


Which I call with:
<?php $word1 = 25 ?>
<?PHP echo maxWords($record['description'], $word1);
?>...<a href="<?php echo $record['_link']; ?>">click for more...</a>
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [gkornbluth] Adding optional formatting to a textbox while using the maxwords function

By Jason - March 16, 2011

Hi Jerry,

Currently, you're using the strip_tags function to remove all HTML tags before you limit the text.

strip_tags() allows you to specify a list of HTML tags that are supposed to be left in. Try changing it to this:

<?PHP
function maxWords($textOrHtml, $maxWords) {

$text = strip_tags($textOrHtml, "<b><i>");
$words = preg_split("/\s+/", $text, $maxWords+1);
if (count($words) > $maxWords) { unset($words[$maxWords]); }
$output = join(' ', $words);

return $output;
}
?>


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/

Re: [Jason] Adding optional formatting to a textbox while using the maxwords function

Hi Jason,

Thanks for looking at this.

I guess my next question is, since these are all text box fields, do I need the strip_tags function at all?

Would I need it if they were WYSIWYG fields instead of text box fields?

If I do need it, what function does it serve? (just curious)

Jerry
The first CMS Builder reference book is now available on-line!







Take advantage of a free 3 month trial subscription, only for CMSB users, at: http://www.thecmsbcookbook.com/trial.php

Re: [gkornbluth] Adding optional formatting to a textbox while using the maxwords function

By Jason - March 17, 2011

Hi,

strip_tags is just for removing HTML from a string. The concern was that if there was a space between a tag and a word, the tag would be counted as a word by the function. If you don't need this, you can certainly remove it. In your case you probably could so long as you know that <b> and <i> are the only tags being used and that there aren't spaces between them.

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/