Sunday, July 8, 2012

Create a word count limit PHP function

<?php

function limit_words($description, $count) {

$words = explode(" ", $description); // explode our string by spaces and add to our $words variable as an array

$chunk = array_chunk($words, $count); // split our $words array into a multi-dimensional array, by our count

$description = implode(" ", $chunk[0]); // convert the $chunk array into a string, seperating words by spaces

return $description; // return our $description string with correct word count

}
$wordcount=$this->limit_words($home_news->intro,15);
?>

No comments:

Post a Comment