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)

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/

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

Thanks Jason,

I'll include this tidbit in the Cookbook as well

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