Location: PHPKode > scripts > Wrappedtext > wrappedtext.php
<?
/* Wrappedtext -wrappedtext.php-

  If a site uses narrow tables this is a great tool because words that are
  longer as the width of a table would enlarge the table and deface the site
  layout. using php's wordwrap function has the disadvantage that the whole
  text is wrapped which means that short words might also get cut at a certain
  position of the line. in addition wordwrap can't be used if the text that
  needs to be wrapped contains html tags.

   Version 0.3
   Last change: 2002/09/06
   copyrigth 2002 Email Communications, http://www.emailcommunications.nl/
   written by Bas Jobsen (hide@address.com)

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program 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 General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/


class wrappedtext
{

	var $string;
	var $maxlength;
	var $length;
	var $endline;



function wrappedtext($string,$maxlength,$length,$endline="\n",$safe_returns=0,$striphtml=1,$allowed_tags='',$maillink=0,$urllink=0,$target='')
{

	$this->string=trim($string)."\n";
	$this->maxlength=$maxlength;
	$this->length=$length;
	$this->endline=$endline;
	$target=empty($target)?'':' target="'.$target.'"';
	if($striphtml)$this->string=strip_tags($this->string,$allowed_tags);
	if($maillink)$this->string=preg_replace('/([_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3}))/i','<a href="mailto:'."\\1".'">'."\\1".'</a>',$this->string);
	if($urllink)$this->string=preg_replace('/((http|https|ftp):\/\/(([A-Z0-9][A-Z0-9_-]*)(\.[A-Z0-9][A-Z0-9_-]*)+)(\/*)(:(\d+))?([A-Z0-9_\/.?~-]*))/i','<a href="'."\\1".'"'.$target.'>'."\\1".'</a>',$this->string);

	if($safe_returns)
	{
	$this->string=str_replace("\n",'<br>',$this->string);
	}


        $this->wordwrap_nonhtml_and_tallwords_only($this->string);
        $this->string=str_replace(array($this->endline.'|'.$this->endline.'|','|'.$this->endline.'|'),$this->endline,$this->string);

}

function wrap_only_tall_words($string,$start,$for=0)
{

		 $tempstring='';
         if(strstr($string,' '))$temparray=explode(' ',$string);
         else $temparray=array($string);
         for($i=0;$i<count($temparray);$i++)
         {
	         if($i>0)$tempstring.=' ';
	         if(strlen($temparray[$i])>$this->maxlength)
	         {
			    if($for)$tempstring.='|'.$this->endline.'|';
	         	$tempstring.=wordwrap($temparray[$i],$this->length,$this->endline,1);
	         }
	         else $tempstring.=$temparray[$i];
         }

return $tempstring;

}

function wordwrap_nonhtml_and_tallwords_only(&$string)
{
$start=0;
$temp='';
$totaal=strlen($string);
$for=0;
while($start<=$totaal)
{

	$begin=strpos($string,'<',$start);

	if($begin===false)
	{
    if(substr($string,$start-strlen($this->endline),strlen($this->endline))!=$this->endline)$temp.=$this->endline;
	$temp.=$this->wrap_only_tall_words(substr($string,$start,$for),$start);
	break;
	}
	else
	{
		if($start!=$begin)
		{
		$temp.=$this->wrap_only_tall_words(substr($string,$start,$begin-$start),$start,$for);
		}
		$end=strpos($string,'>',$begin);
	    $temp.=substr($string,$begin,$end-$begin+1);
	    $start=$end+1;
	}
$for=0;
}
$string=$temp;

}

	function printit()
	{
		echo $this->string;
	}

	function getit()
	{
		return $this->string;
	}


}
?>
Return current item: Wrappedtext