Location: PHPKode > scripts > Perfect URL Trimmer > perfect-url-trimmer/class.trimmer.php
<?php
/************************************************
*	========================================	*
*	Perfect URL Trimmer							*
*	========================================	*
*	Script Name: class.trimmer.php				*
*	Developed By: Khurram Adeeb Noorani			*
*	Email: hide@address.com				*
*	My CV: http://www.visualcv.com/kanoorani	*
*	Twitter: http://www.twitter.com/kanoorani	*
*	Date Created: 05-AUGUST-2009				*
*	Last Modified: 05-AUGUST-2009				*
************************************************/
?>
<?php
class TRIMMER
{
	private $long_url,$service;
	private $services=array("tr.im"=>"tr_im","bit.ly"=>"bit_ly","ur.ly"=>"ur_ly","default"=>"default");
	/*
	We have defined a set of URL trimming services that will be possibly used in this class.
	The default one will be used in last if all of the services fail to run due to any reason.
	It will return the original URL without trimming it.
	*/
	function TRIMMER($service="")
	{
		$this->service=$service;
	}
	function set_service($service)
	{
		/*
		This function will set the specific URL trimming service, so all the URLs afterwards will be trimmed using this service.
		*/
		$this->TRIMMER($service);
	}
	function trim_string($str)
	{
		/*
		The function is used for trimming all the URLs inside the supplied string
		*/
		function substring($str,$start,$end)
		{
			return substr($str,$start,$end-$start);
		}
		/*
		Since PHP doesn't have a function to cut a piece of string between two indexes, so we created one
		*/
		$str=str_replace("\\r","\r",$str);
		$str=str_replace("\\n","\n<BR>",$str);
		$str=str_replace("\\n\\r","\n\r",$str);
		$in=array(
		'`((?:https?|ftp)://\S+[[:alnum:]]/?)`si',
		'`((?<!//)(www\.\S+[[:alnum:]]/?))`si'
		);
		$out=array(
		'~~!!~~!!~~$1!!~~!!~~!!',
		'~~!!~~!!~~http://$1!!~~!!~~!!'
		);
		$str=preg_replace($in,$out,$str);
		/*
		By doing this we mark all the URLs in the string by putting some special characters (~ and !) before and after every URL.
		Now all the URLs in the supplied string will look like ~~!!~~!!~~http://www.google.com!!~~!!~~!!
		*/
		while(strstr($str,"~~!!~~!!~~") && strstr($str,"!!~~!!~~!!"))
		{
			$start=strpos($str,"~~!!~~!!~~");
			$end=strpos($str,"!!~~!!~~!!")+10;
			$long_url=substring($str,$start,$end);
			/*
			So we have got a URL that needs to be trimmed. Right now the URL will look like ~~!!~~!!~~http://www.google.com!!~~!!~~!!
			*/
			$long_url=str_replace(array("~","!"),"",$long_url);
			/*
			Removed the special characters (~ and !) from the URL
			*/
			$short_url=$this->trim_url($long_url);
			/*
			The URL got trimmed
			*/
			$str=str_replace("~~!!~~!!~~$long_url!!~~!!~~!!",$short_url,$str);
			/*
			Finally replace long URL with short URL in the supplied string
			*/
		}
		return $str;
	}
	function trim_url($long_url)
	{
		/*
		This function will trim the supplied long URL
		*/
		$this->long_url=$long_url;
		$short_url=false;
		$do_trim=true;
		foreach($this->services as $service=>$function)
		{
			/*
			This loop checks whether the supplied long URL is already a trimmed URLs.
			If so then there is no need to trim a trimmed URL
			*/
			if(strstr($long_url,"http://$service"))
			{
				$do_trim=false;
				break;
			}
		}
		if($do_trim==true)
		{
			if($this->service=="")
			{
				/*
				If no specific URL trimming service is used then it will go through every service (defined in the array on line #5).
				If the first service fails to trim then it will try second one, and will continue onwards until it gets the URL trimmed.
				*/
				foreach($this->services as $service=>$function)
				{
					if($service!="default")
					{
						$short_url=$this->$function();
						/*
						Calling the function for this service
						*/
						if($short_url!=false)
							break;
						/*
						If the URL has been trimmed successfully then no need to go ahead in the loop
						*/
					}
				}
			}
			else
			{
				/*
				If a specific URL trimming service is used then it will call its respective function
				*/
				$service=$this->service;
				if($service!="default")
				{
					$function=$this->services[$service];
					$short_url=$this->$function();
					/*
					Calling the function for this service
					*/
				}
			}
		}
		if($short_url==false)
			$short_url=$long_url;
		/*
		This is the final punch, if all the services resulted in failure then we will return the original URL.
		But this point is very rare and exceptional as we have three services and there is only %1 chance of failure of all services.
		*/
		return $short_url;
	}
	function create_xml_request($xml_url,$params)
	{
		/*
		This function will create an XML request URL with supplied parameters
		*/
		$xml_request=$xml_url."?";
		foreach($params as $key=>$value)
		{
			$xml_request.="$key=$value&";
		}
		return $xml_request;
	}
	function tr_im()
	{
		/*
		This function will trim the URL using the API of http://tr.im
		Complete API documentation of tr.im can be found here http://tr.im/website/api
		*/
		$long_url=urlencode($this->long_url);
		$xml_url="http://api.tr.im/api/trim_url.xml";
		$params=array("url"=>$long_url);
		/*
		At the moment only mandatory parameter is used here, i.e. url
		There are more (optional) parameters such as username, password, etc
		For more information about parameters, please refer to the API documentation at http://tr.im/website/api
		*/
		$xml_request=$this->create_xml_request($xml_url,$params);
		/*
		Create a URL for sending XML request. It will look like http://api.tr.im/api/trim_url.xml?url=http://www.google.com
		*/
		$xml=simplexml_load_file($xml_request);
		if(isset($xml->status))
		{
			$code=(int)$xml->status->attributes()->code;
			/*
			The full list of possible codes can be found here http://tr.im/website/api
			*/
			if($code==200 || $code==201 || $code==202 || $code==205)
				return (string)$xml->url;
			else
				return false;
		}
		else
			return false;
	}
	function bit_ly()
	{
		/*
		This function will trim the URL using the API of http://bit.ly
		Complete API documentation of bit.ly can be found here http://code.google.com/p/bitly-api/wiki/ApiDocumentation
		*/
		$long_url=urlencode($this->long_url);
		$xml_url="http://api.bit.ly/shorten";
		$params=array("version"=>"2.0.1","longUrl"=>$long_url,"login"=>"bitlyapidemo","apiKey"=>"R_0da49e0a9118ff35f52f629d2d71bf07","format"=>"xml");
		/*
		There are no other (optional) parameters in bit.ly API
		For more information about parameters, please refer to the API documentation at http://code.google.com/p/bitly-api/wiki/ApiDocumentation
		*/
		$xml_request=$this->create_xml_request($xml_url,$params);
		/*
		Create a URL for sending XML request. It will look like http://api.bit.ly/shorten?version=2.0.1&longUrl=http://www.google.com&login=bitlyapidemo&apiKey=R_0da49e0a9118ff35f52f629d2d71bf07&format=xml
		*/
		$xml=simplexml_load_file($xml_request);
		if(isset($xml->errorCode))
		{
			$code=(int)$xml->errorCode;
			/*
			On success the errorCode is 0
			*/
			if($code==0)
				return (string)$xml->results->nodeKeyVal->shortUrl;
			else
				return false;
		}
		else
			return false;
	}
	function ur_ly()
	{
		/*
		This function will trim the URL using the API of http://ur.ly
		Complete API documentation of ur.ly can be found here http://code.google.com/p/urly/wiki/APIDocumentation
		*/
		$long_url=urlencode($this->long_url);
		$xml_url="http://ur.ly/new.xml";
		$params=array("href"=>$long_url);
		/*
		There are no other (optional) parameters in ur.ly API
		For more information about parameters, please refer to the API documentation at http://code.google.com/p/urly/wiki/APIDocumentation
		*/
		$xml_request=$this->create_xml_request($xml_url,$params);
		/*
		Create a URL for sending XML request. It will look like http://ur.ly/new.xml?href=http://www.google.com
		*/
		$xml=simplexml_load_file($xml_request);
		if(isset($xml->attributes()->code))
		{
			$code=(string)$xml->attributes()->code;
			/*
			There is no success or error code in the response XML
			In this case, the code refers to the URL suffix of the trimmed URL, i.e. http://ur.ly/XXXX
			*/
			return "http://ur.ly/$code";
		}
		else
			return false;
	}
}
?>
Return current item: Perfect URL Trimmer