Login   Register  
PHP Classes
elePHPant
Icontem

File: linker.class.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of BrettsBSD.net  >  linker.class.php  >  linker.class.php  >  Download  
File: linker.class.php
Role: ???
Content type: text/plain
Description: Linker Class File
Class: linker.class.php
Author: By
Last change:
Date: 2001-05-18 01:08
Size: 3,194 bytes
 

Contents

Class file image Download
<?
// Brett D. Estrade
// estrabd@mvdev.com
// http://www.mvdev.com

/**
*	This class parses a string, and automatically
*	formats URLs and email addresses with the 
*	appropriate HTML so that they are linked.
*
*	It would be easy to extend automatic formatting
*	to other items, but currently my needs are only 
*	for URLs and email address.
*
*	If any improvments are made, please let me know, and
*	I will include them in my distribution.
*
*	This code is free for use, modification, and distribution
*	as long as this header remains up here.
*
*	Regular expressions for pattern matching were borrowed from 
*   http://www.phpwizards.net .
*/

class Linker
{

	/**
	* Constructor -- initializes internal elements
	*/

	function Linker()
	{
		//String to parse
		$this->str = '';
		//Unmodified string
		$this->oldStr = '';
		/////
		//	The below member variables define the link formatting
		//  for example, for an email address, the default format ('mail:')would
		//  produce a regular email link, as in <a href="mailto:estrabd@mvdev.com">estrabd@mvdev.com</a>,
		//  but say you had a page called 'contact.php' that produced a form using 'sendTo' in the query 
		//  string which you used to pass the email address, you would set the email format below
		//  to './contact.php?sendTo=' .  This produces a link such as 
		//  <a href="./contact.php?sendTo=estrabd@mvdev.com">
		////
				
		//URL link format
		$this->urlFormat = 'href=';
		//URL link target
		$this->urlTarget = '_self';
		//Email link format
		$this->emailFormat = './contact.php?sendTo=';//'mailto:';
		// Query string example: 
		//$this->emailFormat='./contact.php?sendTo=';
		//Email link target
		$this->emailTarget = '_self';
	}
	
	/**
	* Main method --> does parsing and replacing.
	*/
	
	function format($str = '')
	{
		$this->str = $str;
		$this->oldStr = $str;
		if (! $this->str == '')
		{
		
		// Regex to parse URLs.
		// Contributed by Bill Peck, http://www.pecknet.com
		// bill@pecknet.com
		// This leaves URLs that already have the HTML formatting to be left alone
		// For example, it will parse 'http://www.mvdev.com',
		// but not '<a href="http://www.mvdev.com">mvdev's site</a>'
		$this->str = eregi_replace("([^\"[[:alpha:]]|^)([[:alpha:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])", "\\1<a ".$this->urlFormat."\"\\2://\\3\\4\"target=\"".$this->urlTarget."\">\\2://\\3\\4</a>", $this->str);    	
		//These regular expressions were borrowed from http://www.phpwizard.net .  Thanks, guys!
		//old regex for URLS, has been replaced with the on above
		//$this->str = eregi_replace("([[:alnum:]]+)://([^[:space:]]*)([[:alnum:]#?/&=])", "<a ".$this->urlFormat."\"\\1://\\2\\3\" target=\"_blank\" target=\"".$this->urlTarget."\">\\1://\\2\\3</a>", $this->str);
                $this->str = eregi_replace("(([a-z0-9_]|\\-|\\.)+@([^[:space:]]*)([[:alnum:]-]))", "<a href=\"".$this->emailFormat."\\1\" target=\"".$this->emailTarget."\">\\1</a>", $this->str);
                return $this->str;
		}
	}

	/**
	* Returns original string that was parsed.
	*/	

	function getOldStr()
	{
		return $this->oldStr;
	}
}

?>