+44 (0)785 715 1348
office@bogdans.co.uk

WordPress function to limit the number of characters 0

Posted on 9, September 2013

in Category Web development


This function limits a string down to a number of characters. The last parameter checks if the last word gets split or not.

function limit_number_characters_string($string, $number, $words){

	if ($words == true){

		if (strlen($string) > $number){

			$string = substr($string, 0, $number);

			for ($counter = strlen($string); $counter > 0; $counter--){
				if (substr($string, $counter - 1, 1) == " "){
					return trim($string) . "..";
				} else {
					$string = substr($string, 0, -1);
				}
				
			}
			
		}
					
		return $string;
		
	} else {

		if (strlen($string) > $number){
			return substr($string, 0, $number) . "..";
		} else {
			return $string;
		}
		
	}

}

0 Comments