Location: PHPKode > scripts > simpleTagReader > simpletagreader/class.simpleTagReader.php
<?php

/**
* Armin Randjbar-Daemi <www.omnistream.co.uk>
* 19 May 2008
*/

class simpleTagReader {
	
	var $fileContent;
	
	function simpleTagReader($filename, $fileLen=5000)
	{
		$handle = @fopen($filename, "r");
		$this->fileContent =fread($handle, $fileLen);
		fclose($handle);
	}

	function readAllTags($tagName)
	{
		$tagLen = strlen($tagName);
		$content = $this->fileContent;
		do {
			$tagStart = "<" . $tagName . ">";
			$tagStart = strpos($content, $tagStart);
		
			$tagEnd = "</" . $tagName . ">";
			$tagEnd = strpos($content, $tagEnd);

			if ($tagStart != False)
			{
				$results[] = trim(substr($content, $tagStart+$tagLen+2, $tagEnd-$tagStart-$tagLen-2));
				$content = substr($content, $tagEnd+$tagLen+3);
			}
		} while ($tagStart!=False);

		return $results;
	}
	
	//Read 1 Tag: Tag number $tagNum
	function readTag($tagName, $tagNum)
	{
		$tagCounter = 0;
		$tagLen = strlen($tagName);
		$content = $this->fileContent;
		do {
			$tagStart = "<" . $tagName . ">";
			$tagStart = strpos($content, $tagStart);
		
			$tagEnd = "</" . $tagName . ">";
			$tagEnd = strpos($content, $tagEnd);

			if ($tagStart != False)
			{
				if ($tagCounter+1 == $tagNum) {
					return trim(substr($content, $tagStart+$tagLen+2, $tagEnd-$tagStart-$tagLen-2));
				} else {
					$content = substr($content, $tagEnd+$tagLen+3);
					$tagCounter++;
				}
			}
		} while ($tagStart!=False);
	}

}//Class End
?>
Return current item: simpleTagReader