Location: PHPKode > projects > BBCode Advanced > bbcode-advanced/lib/class.manageText.php
<?php
#########################################################
#	GiveMeEnergy Projects								#
#	Engine by []==ThoR®	for More Interactive			#
#	Contact:											#
#	hide@address.com									#
#														#
#########################################################

/*
	$Id: class.manageText.php,v 1.16 2006/02/03 09:23:47 thor Exp $
*/

/*
Ex.
$string	= ManageString::BR($string,1,1);
$string	= ManageString::MakeUrl($string);
$string	= ManageString::Highlight($string,$highlight);

To call others functions you need to initialize the class
$mString = new ManageString();
$string = $mString->BadWords($string);
$string = $mString->Smiles($string);
*/

class ManageString	{

	var $smile_path			= 'img/smiles';					//	Smiles path img.
	var $badWordsList		= Array(						//	Filter Bad Words.
								"fuck",
								"ass",
								"shit");
	var $replacement		= " **** ";						//	Replace badWord whit this one.
	var $smileAsciiList		= Array(						//	emoticons in ASCII mode.
								":-)"
								,":)"
								,":-D"
								,":-("
								,":("
								,":-O"
								,":O"
								,"8)"
								,":P"
								,">:-<"
								);
	var $smileImagesList	= Array(						//	emoticons in IMAGE mode.
								"smile.gif"
								,"smile.gif"
								,"laugh.gif"
								,"sad.gif"
								,"sad.gif"
								,"surprised.gif"
								,"surprised.gif"
								,"cool.gif"
								,"razz.gif"
								,"evil.gif"
								);

	//	Replace all à,ò,è,etc. with HTML code
	//	@public
	//	return string
	Function StripString($string,$standard=0,$utf8=0)	{
		//	Standard Replace	//
		$search		= Array("/’/","/“/","/</","/>/","/\"/");
		$replace	= Array("\'","\'","&lt;","&gt;","&quot;");
		$string		= ($standard == 1)	? preg_Replace($search,$replace,$string)	: $string;
		//	UTF 8 				//
		$search		= Array("/à/","/á/","/ù/","/ò/","/è/","/é/","/ì/");
		$replace	= Array("&aacute;","&agrave;","&ugrave;","&ograve;","&egrave;","&eacute;","&igrave;");
		$string		= ($utf8 == 1)	? preg_Replace($search,$replace,$string)	: $string;
		$string		= (get_magic_quotes_gpc() == 0)	? addslashes($string) : $string;
		return $string;
	}

	//	Replace \n\t\r with HTML code
	//	@public
	//	return string
	Function BR($string,$n=0,$t=0,$r=0)	{
		//	\n\r\t				//
		$string		= ($n == 1)	? Str_Replace("\n","<br>",$string)							: $string;
		$string		= ($r == 1)	? Str_Replace("\r","<br>",$string)							: $string;
		$string		= ($t == 1)	? Str_Replace("\t","&nbsp;&nbsp;&nbsp;&nbsp;",$string)		: $string;
		return $string;
	}

	//	Replace possible links with HTML code <a href=\"http://:ftp://mailto:\">
	//	@public
	//	return string
	Function MakeUrl($string)	{
		//	Make Url (http, ftp, mailto)	//
		$search		= array("/(?<!quot;|[=\"]|:\/{2})\b((\w+:\/{2}|www\.).+?)"
					."(?=\W*([<>\s]|$))/i", "/\b(^| )([\w\.]+)(@)([\w\.-]+)\b/i");
		$replace	= array("<a href=\"$1\">$1</a>", "<a href=\"mailto:$0\">$0</a>");
		return preg_Replace("/href=\"www/i", "href=\"http://www", preg_Replace ($search, $replace, $string));
	}

	//	Highlight a word in a given string
	//	@public
	//	return string
	Function Highlight($string,$highlight)	{
		//	Highlight, usually used for search	//
		###	HighLight only if word is longer than 3 letters in total	###
		If ( ($highlight) && (StrLen($highlight) > 3)	)	{
			$search		= Array();
			$replace 	= Array();
			###	Split if more than one word								###
			If (StriStr($highlight," ") != "")	{
				$highlight	= Split(" ",$highlight);
				foreach($highlight as $searchforIt)	{
					###	If word has more than 3 letters	per word		###
					If (StrLen($searchforIt) > 3)	{
						###	Prepare the words array						###
						$keyWord	= "/".$searchforIt."/si";			//	si = case insensitive
						Array_Push($search,$keyWord);
					}
				}
				###	Prepare the replacemente array						###
				$words		= Count($search);
				for($i=0;$i<=$words;$i++)	{
					Array_Push($replace,"<span class=\"Highlight\">\\0</span>");
				}
			} else {
				###	Only One Word To search/replace						###
				Array_Push($search,"/".$highlight."/si");				//	si = case insensitive
				Array_Push($replace,"<span class=\"Highlight\">\\0</span>");
			}
			###	Return replaced String									###
			return preg_Replace($search, $replace, $string);
		} else {
			###	No word to search/replace. Return Original String		###
			return $string;
		}
	}

	//	Use Smiles (replace :), :P, et.c with images)
	//	@public
	//	return string
	//	2005-07-25: fixed a little bug by Fabrizio Parrella
	//	Don't use stripslahes() after used this function
	Function Smiles($string)	{
		$smileAscii		= $this->smileAsciiList;
		$smileImages	= $this->smileImagesList;

		###	from phpBB functions	###
		for ($i = 0; $i < count($smileAscii); $i++)	{
			$theSmile	= $smileAscii[$i]; 
			/*	Modified by Fabrizio Parrella	*/
			$theSmile	= str_replace('"', '\"', $theSmile); 
			$theSmile	= preg_quote($theSmile); 
			$theSmile	= str_replace("/", "\\/", $theSmile); 
			$orig[]		= '/\['.$theSmile.'\]/si'; 
			$repl[]		= '<img src="'.$this->smile_path.'/'.$smileImages[$i].'" alt="'.$smileAscii[$i].'" border="0" />'; 
		}
		$message	= preg_Replace($orig, $repl,' '.$string.' ');
		return $message;
		//return Preg_Replace($smileAscii, $smile_path."/".$smileImages, $string);
	}

	//	Censore bad words
	//	@public
	//	return string
	//	2005-07-25: fixed a little bug by Fabrizio Parrella
	Function BadWords($string)	{
		$badWords		= $this->badWordsList;
		$replacement	= $this->replacement;
		$search			= Array();
		foreach($badWords as $searchforIt)	{
			//	2005-07-25
			$searchforIt	= str_replace('"', '\"', $searchforIt);
			$searchforIt	= preg_quote($searchforIt);
			$searchforIt	= str_replace("/", "\\/", $searchforIt);
			$search[]		= '/([^\w]\W?)('.$searchforIt.')([^\w]\W?)/si';
			//	Old method
			//$keyWord	= "/".$searchforIt."/si";			//	si = case insensitive
			//Array_Push($search,$keyWord);
		}
		//	old method
		//$message		= preg_Replace($search, $replacement, $string);
		//	2005-07-25
		$message		= preg_replace($search, '\\1'.$replacement.'\\3', ' '.$string.' ');
		return $message;
	}

	//	BBCode: replace bold, italic and so on in the text
	//	include BBCode.php in the page
	//	@public
	//	return string
	function theBBCode($string)	{
		$string	= bbCode::parseBBCode($string);
		return $string;
	}
}

?>
Return current item: BBCode Advanced