Scripts

Tekst afkorten op woorden

Je gebruikt de functie als volgt:

tekst-afkorten-op-woorden
<?
function ShortTxt($tekst, $num = '')
{
    //-- if $num is empty, set $num to 25
    if (!$num)
		$num = 25;

	//-- count words
	$words = explode(" ", $tekst);
	$total = count($words);
	
	//-- if there are $num or more words
	if ($total >= $num)
	{
		unset($i);
		
		//-- cut on words, not on letters
		for ($i = 0; $i <= $num; $i++)
		{
			if (!$short)
				$short = $words[$i];
			else
				$short = $short . " " . $words[$i];
		}
		
		//-- add "..." to it
		$short = $short . "...";
	}
	//-- less then 30 words, show all
	else
		$short = $tekst;
	
	//-- return the ajusted (or not) text
	return $short;
}
?>

Reacties

0
Nog geen reacties.