Monday, 12 November 2012

php - Limiting characters retrived from a database field

<?php
$text = '<p>Test paragraph.</p><!-- Comment --> <a href="#fragment">Other text</a>';
echo $string=strip_tags($text);
echo "<br>";
echo str_word_count(strip_tags($text));
echo "<br>";
echo strlen(strip_tags($text));
echo "<br>";
echo limit_words($string, 2);
echo "<br>";
function limit_words($string,$charecter_limit){
$string=substr($string,0,$charecter_limit);
$words = explode(" ",$string);
return implode(" ",array_splice($words,0,count($words)-1));
}
?>

No comments:

Post a Comment