note 73604 modified in function.str-word-count by danbrown

From: Date: Fri, 31 Aug 2012 16:26:29 +0000
Subject: note 73604 modified in function.str-word-count by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-190369@lists.php.net to get a copy of this message
I needed a function which would extract the first hundred words out of a given input while retaining all markup such as line breaks, double spaces and the like. Most of the regexp based functions posted above were accurate in that they counted out a hundred words, but recombined the paragraph by imploding an array down to a string. This did away with any such hopes of line breaks, and thus I devised a crude but very accurate function which does all that I ask it to:

<?php
function Truncate($input, $numWords) 
{
 if(str_word_count($input,0)>$numWords)
 {
	$WordKey = str_word_count($input,1);
	$PosKey = str_word_count($input,2);
	reset($PosKey);
	foreach($WordKey as $key => &$value)
	{
		$value=key($PosKey);
		next($PosKey);
	}
	return substr($input,0,$WordKey[$numWords]);
 }
 else {return $input;}
}
?>

The idea behind it? Go through the keys of the arrays returned by str_word_count and associate the number of each word with its character position in the phrase. Then use substr to return everything up until the nth character. I have tested this function on rather large entries and it seems to be efficient enough that it does not bog down at all.

Cheers!

Josh --was-- I needed a function which would extract the first hundred words out of a given input while retaining all markup such as line breaks, double spaces and the like. Most of the regexp based functions posted above were accurate in that they counted out a hundred words, but recombined the paragraph by imploding an array down to a string. This did away with any such hopes of line breaks, and thus I devised a crude but very accurate function which does all that I ask it to: function Truncate($input, $numWords) { if(str_word_count($input,0)>$numWords) { $WordKey = str_word_count($input,1); $PosKey = str_word_count($input,2); reset($PosKey); foreach($WordKey as $key => &$value) { $value=key($PosKey); next($PosKey); } return substr($input,0,$WordKey[$numWords]); } else {return $input;} } The idea behind it? Go through the keys of the arrays returned by str_word_count and associate the number of each word with its character position in the phrase. Then use substr to return everything up until the nth character. I have tested this function on rather large entries and it seems to be efficient enough that it does not bog down at all. Cheers! Josh http://php.net/manual/en/function.str-word-count.php

« previous php.notes (#190369) next »