Return to Snippet

Revision: 52490
at October 26, 2011 06:10 by MichaelM


Updated Code
function limitWords($str,$limit){
	$wc 	= explode(' ',$str);
	$c 	= count($wc);
	$r = '';
	if($c <= $limit)
		$r = $str;
	else{
		$i=0;
		while($i<$limit){
			$r .= $wc[$i].' ';
			$i++;
		}
	}
	return $r;
}

Revision: 52489
at October 25, 2011 02:45 by MichaelM


Initial Code
function limitWords($str,$limit){
	$wordCount 	= explode(' ',$str);
	$count 		= count($wordCount);
	$return 	= '';
	if($count <= $limit)
		$return = $str;
	else{
		$i=0;
		while($i<$limit){
			$return .= $wordCount[$i].' ';
			$i++;
		}
	}
	return $return;
}

Initial URL

                                

Initial Description
Example:

echo limitWords('This is my string personally i think its too long',2);

Will return "This is"

Initial Title
PHP Limit Words

Initial Tags

                                

Initial Language
PHP