Location: PHPKode > scripts > SEO Title > seo-title/seoTitle.php
<?
/**
 * SeoTitle.php :: Seo Title Class
 *
 * Class version 1.0.0.0
 * copyright (c) 2009 by Sandeep Kumar
 * Seo Title Class is an open source PHP class library for easily know how much title of your web page is suitable to seo. 
 
 * SeoTitle is released under the terms of the LGPL license
 * http://www.gnu.org/copyleft/lesser.html#SEC3
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * @package SEO
 * @copyright Copyright (c) 2009-20010  by Sandeep Kumar
 * @license http://www.gnu.org/copyleft/lesser.html#SEC3 LGPL License
 */
class Seo_Title
{
	function getTitle($url)
	{
		$url=trim($url);
		$url=str_replace('http://','',$url);
		$url='http://'.$url;
		$this->url=$url;
		$res=array();
		$handle = @fopen($url, "r");
		if($handle)
		{
			$page = '';
			while (!feof($handle)) 
			{
				$page .= fread($handle, 8192);
			}
		}
		else
		{
			echo "URL not Valid: ".$url."<br>";
			exit;
		}
		
		$info = parse_url($url);
		$urls = array();
		$page=strtolower($page);
		$title ='';
		if (preg_match("/<title>(.*)<\/title>/isU",$page,$ar)) 
		{
			$title = $ar[1];
		}
		
		return trim($title);
	}


	
	
	function seoTitle($url)
	{
		$title=$this->getTitle($url);
		$result=array();
		$result['titleText']=$title;
		$result['title']=$this->checkTitle($title);
		return $result;
	}
	function checkTitle($titleText="")
	{
		$titleLength=strlen($titleText);
		$keywordPosition=0;
		$isKeywordExist=false;
		$countEachValue=array(0=>"1",1=>"1",2=>"1");
			// total # of seperator
			$seperator["bar"]["total"]=substr_count($titleText,"|");
			$seperator["dash"]["total"]=substr_count($titleText,"-");
			$seperator["colon"]["total"]=substr_count($titleText,":");
			$seperator["greater"]["total"]=substr_count($titleText,">");
			
			// last occurance of each seperator
			$lastIndex=0;
			$sep='=';
			$seperator["bar"]["last"]=strrpos($titleText,"|");
			if($seperator["bar"]["last"]>$lastIndex)
			{
			$lastIndex=$seperator["bar"]["last"];
			$sep='|';
			}
			$seperator["dash"]["last"]=strrpos($titleText,"-");
			
			if($seperator["dash"]["last"]>$lastIndex)
			{
			$lastIndex=$seperator["dash"]["last"];
			$sep='-';
			}
			$seperator["colon"]["last"]=strrpos($titleText,":");
			
			if($seperator["colon"]["last"]>$lastIndex)
			{
			$lastIndex=$seperator["colon"]["last"];
			$sep=':';
			}
			$seperator["greater"]["last"]=strrpos($titleText,">");
			
			if($seperator["greater"]["last"]>$lastIndex)
			{
			$lastIndex=$seperator["greater"]["last"];
			$sep='>';
			}
		
		if($seperator["bar"]["total"]>0) $isKeywordExist=true; 
		if($seperator["dash"]["total"]>0) $isKeywordExist=true; 
		if($seperator["colon"]["total"]>0) $isKeywordExist=true; 
		if($seperator["greater"]["total"]>0) $isKeywordExist=true; 
		
	   $splitTitle=explode($sep,$titleText);
		   if($seperator["bar"]["total"]>0 and $sep!='|')
		   {
				$splitTitle[0]=explode("|",$splitTitle[0]);
		   }
			if($seperator["dash"]["total"]>0 and $sep!='-')
		   {
				$splitTitle[0]=explode("-",$splitTitle[0]);
		   }
			if($seperator["greater"]["total"]>0 and $sep!='>')
		   {
				$splitTitle[0]=explode(">",$splitTitle[0]);
		   }
		   
		   if($seperator["colon"]["total"]>0 and $sep!=':')
		   {
				$splitTitle[0]=explode(":",$splitTitle[0]);
		   }
			
		
			$left=count($splitTitle[0])."<br><br>";
			$right=count($splitTitle[1]);		
			$keywordLeftSide=false;
			$keywordRightSide=false;
			$unableToPredict=false;
			if($left>1 and $right==1)
			{
				$keywordLeftSide=true;
				//use this to check repeated keywords
				$countEachValue=array_count_values($splitTitle[0]);
			}
			if($left==1 and $right>1)
			{
				$keywordRightSide=true;
				//use this to check repeated keywords
				$countEachValue=array_count_values($splitTitle[1]);
			}
			
			if($left==1 and $right==1)
			{
				//use this to check repeated keywords
				//$countEachValue=array_count_values($splitTitle[0]);
				// predict by checking length
				if(strlen($splitTitle[0])>strlen($splitTitle[1]))
				$keywordRightSide=true;
				
				if(strlen($splitTitle[0])<strlen($splitTitle[1]))
				$keywordLeftSide=true;
			
				if(strlen($splitTitle[0])==strlen($splitTitle[1]))
				$unableToPredict=true;
											
			}
			
			//Now check all condition one by one
			
			//If the title is <64 and the keyword is located first in order, then the text “Title Text 1” shows.
			if($titleLength<64 and $keywordLeftSide==true)
			{
				$result="the title is <64 and the keyword is located first in order";
			}
			
			//If the title is <64 and the keyword is located but NOT as the first word in order, then the text “Title Text 2” shows. And you receive a score of -
			
			if($titleLength<64 and $keywordRightSide==true)
			{
				$result="the title is <64 and the keyword is located but NOT as the first word in order";
			}
			
			// If the title is <64 and the keyword is NOT located at all, then the text “Title Text 3” shows. And you receive a score of –
			if($titleLength<64 and $isKeywordExist==false)
			{
				$result="the title is <64 and the keyword is NOT located at all";
			}
		
			//If the title is <64 and the keyword is located more than 1 time in the title , then the text “Title Text 4” shows. And you receive a score of  –
				if($titleLength<64 and array_filter($countEachValue,"multipleEntries"))
				{
					$result="the title is <64 and the keyword is located more than 1 time in the title";
				}
		
			//If the title is >64 and the keyword is located first in order, then the text “Title Text 5” shows.
			if($titleLength>64 and $keywordLeftSide==true)
			{
				$result="the title is >64 and the keyword is located first in order";
			}
			
			//If the title is >64 and the keyword is located but NOT as the first word in order, then the text “Title Text 6” shows. And you receive a score of -
			
			if($titleLength>64 and $keywordRightSide==true)
			{
				$result="the title is >64 and the keyword is located but NOT as the first word in order";
			}
			
			// If the title is >64 and the keyword is NOT located at all, then the text “Title Text 7” shows. And you receive a score of –
			if($titleLength>64 and $isKeywordExist==false)
			{
				$result="the title is >64 and the keyword is NOT located at all";
			}
		
			//If the title is >64 and the keyword is located more than 1 time in the title , then the text “Title Text 8” shows. And you receive a score of  –
				if($titleLength>64 and count($countEachValue)>0 and array_filter($countEachValue,"multipleEntries"))
				{
					$result="the title is >64 and the keyword is located more than 1 time in the title";
				}
			
			// If a title is NOT located at all, then the text “Title Text 9” shows. And you receive a score of  –	
				if($right==0 and $splitTitle[0]=='')
				{
					$result=" a title is NOT located at all";
				}
					
		/*}*/
		return $result;
	}	
	
}

// put this function out side this class
function multipleEntries($v)
	{
		if ($v==1)
  		{
  			return false;
  		}
			return true;
	}

?>
Return current item: SEO Title