apply htmlspecialchars to maxWords

3 posts by 2 authors in: Forums > CMS Builder
Last Post: June 22, 2011   (RSS)

By Mikey - June 22, 2011

Is it possible to apply htmlspecialchars to maxWords?
Header Code
<?PHP
function maxWords($html, $maxWords) {
$html = str_replace("<li>","<p>",$html);
$html = str_replace("</li>","</p>",$html);
$html = str_replace("<p>","*P*",$html);
$html = str_replace("</p>","*/P*",$html);
$html = str_replace("<"," <",$html);
$text = strip_tags($html);
$words = preg_split("/\s+/", $text, $maxWords+1);
if (count($words) > $maxWords) { unset($words[$maxWords]); }
$output = join(' ', $words);
$output=str_replace("*P*","<p>",$output);
$output=str_replace("*/P*","</p>",$output);
$output.="</p>";

return $output;
}
?>


Page Code
<a href="<?php echo htmlspecialchars($record['_link']); ?>"><?php echo maxWords($record['media'], 20); ?></a>

Re: [Jason] apply htmlspecialchars to maxWords

By Mikey - June 22, 2011

Hi,

Do you mean you want to apply htmlspecialchars to the output of maxWords, or to the HTML that you're passing into it?

You can do both, For example:

<?php echo htmlspecialchars(maxWords($record['media'], 20)); ?>

OR

This is what I needed... Thanks Jason!
<?php echo maxWords(htmlspecialchars($record['media']), 20); ?>

Is this what you're looking for?

Hope this helps