Location: PHPKode > scripts > Parse keywords from text > parse-keywords-from-text/text.class.php
<?
/*
Copyright: 2009 êîò, àäìèí, øðåäåð.
Author: ß àöêèé îäìèí, éà ñ÷àñ â ñåðâåðíîé êîòà ÷åðåç øðåäåð çà 25 ñåêíä ïðîïóñòèë o_0
*/
class Text
{
	private $text = '';
	private $temp_words = '';
	private $words = '';

	function __construct ($text_file, $min_strlen = 4)
	{
		if (file_exists ($text_file))
		{
		$this->text = file_get_contents ($text_file);
		}
		else
		{
			throw new Exception("File $text_file not found");
		}
	$this->text2words ();
	$this->gen_words ($min_strlen);
	}

	private function text2words ()
	{
	preg_match_all ('/(\w+)/i', $this->text, $this->temp_words);
	$this->temp_words = $this->temp_words ['0'];
	}

	private function gen_words ($min_strlen)
	{
		foreach ($this->temp_words as $key=>$value)
		{
			if (strlen ($value) <= $min_strlen)
			{
			unset ($this->temp_words[$key]);
			}
			else
			{
			$value = strtolower ($value);
				if (isset ($this->words[$value]))
				{
				$this->words[$value] = $this->words[$value] + 1;
				}
				else
				{
				$this->words[$value] = 1;
				}
			}
		}
	$this->temp_words = null;
	arsort ($this->words);
	}

	public function get_limit ($a, $b)
	{
		if ((int) $b==="0")
		{
			return false;
		}

		return array_slice ($this->words, (int) $a, $b);
	}

	public function get_count_limit ($a)
	{
	$a = (int) $a;
		foreach ($this->words as $key=>$value)
		{
			if ($value >= $a)
			{
			$temp[$key] = $value;
			}
			else
			{
			break;
			}
		}
		return $temp;
	}

	public function get_all ()
	{
		return $this->words;
	}
}
Return current item: Parse keywords from text