note 18416 modified in function.wordwrap by zak

From: Date: Tue, 22 Jan 2002 14:17:44 +0000
Subject: note 18416 modified in function.wordwrap by zak
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-25693@lists.php.net to get a copy of this message
Wordwrap nonproportional fonts! and exclude html tags! If you have a problem with PHP' worwrap which you aren't able to give arguments for NOT parsing through some string parts (like html codes, which could be longer then ie.80 chars), here's how to do it. This function, cuts words in $str longer then $cols long, and puts and $cut in it. It escapes everything whitin "<>". So "<longggggggggggggtext>" will not be cutted, and just "longggggggggggggtext", will be cutted! :) This one can also try to work with nonproportional fonts. Like Arial and Verdana, instead of only fixed fonts. $str = the string you want to process. $cols = choose ie.80 for prop.fonts choose ie.450 for non prop fonts NOTE: $cols are not pixels! $non_prop = choose "true" for arial,verdana etc. false = fixed (courier) $exclude1 = excludesystem, read the begin of this note. enter '<' $exclude2 = enter '>' function sheep_wordwrap($str,$cols,$non_prop,$cut,$exclude1,$exclude2){ $count=0; $tagcount=0; $str_len=strlen($str); //$cut=" $cut "; $calcwidth=0; for ($i=1; $i<=$str_len;$i++){ $str_len=strlen($str); if ($str[$i]==$exclude1) $tagcount++; elseif ($str[$i]==$exclude2){ if ($tagcount>0) $tagcount--; } else{ if (($tagcount==0)){ if (($str[$i]==' ') || ($str[$i]=="\n")) $calcwidth=0; else{ if ($non_prop){ if (ereg("([QWOSDGCM#@m%w]+)",$str[$i],$matches)) $calcwidth=$calcwidth+7; elseif (ereg("([I?\|()\"]+)",$str[$i],$matches)) $calcwidth=$calcwidth+4; elseif (ereg("([i']+)",$str[$i],$matches)) $calcwidth=$calcwidth+2; elseif (ereg("([!]+)",$str[$i],$matches)) $calcwidth=$calcwidth+3; else{ $calcwidth=$calcwidth+5; } } else{ $calcwidth++; } if ($calcwidth>$cols){ $str=substr($str,0,$i-1).$cut.substr($str,$i,$str_len-1); $calcwidth=0; } } } } } return $str; //moby rules at 5am! :) } --was-- >>> Could someone at PHP, please delete my last 2 messages, and delete THIS line only? >>> <<< Wordwrap nonproportional fonts! and exclude html tags! If you have a problem with PHP' worwrap which you aren't able to give arguments for NOT parsing through some string parts (like html codes, which could be longer then ie.80 chars), here's how to do it. This function, cuts words in $str longer then $cols long, and puts and $cut in it. It escapes everything whitin "<>". So "<longggggggggggggtext>" will not be cutted, and just "longggggggggggggtext", will be cutted! :) This one can also try to work with nonproportional fonts. Like Arial and Verdana, instead of only fixed fonts. $str = the string you want to process. $cols = choose ie.80 for prop.fonts choose ie.450 for non prop fonts NOTE: $cols are not pixels! $non_prop = choose "true" for arial,verdana etc. false = fixed (courier) $exclude1 = excludesystem, read the begin of this note. enter '<' $exclude2 = enter '>' function sheep_wordwrap($str,$cols,$non_prop,$cut,$exclude1,$exclude2){ $count=0; $tagcount=0; $str_len=strlen($str); //$cut=" $cut "; $calcwidth=0; for ($i=1; $i<=$str_len;$i++){ $str_len=strlen($str); if ($str[$i]==$exclude1) $tagcount++; elseif ($str[$i]==$exclude2){ if ($tagcount>0) $tagcount--; } else{ if (($tagcount==0)){ if (($str[$i]==' ') || ($str[$i]=="\n")) $calcwidth=0; else{ if ($non_prop){ if (ereg("([QWOSDGCM#@m%w]+)",$str[$i],$matches)) $calcwidth=$calcwidth+7; elseif (ereg("([I?\|()\"]+)",$str[$i],$matches)) $calcwidth=$calcwidth+4; elseif (ereg("([i']+)",$str[$i],$matches)) $calcwidth=$calcwidth+2; elseif (ereg("([!]+)",$str[$i],$matches)) $calcwidth=$calcwidth+3; else{ $calcwidth=$calcwidth+5; } } else{ $calcwidth++; } if ($calcwidth>$cols){ $str=substr($str,0,$i-1).$cut.substr($str,$i,$str_len-1); $calcwidth=0; } } } } } return $str; //moby rules at 5am! :) } http://www.php.net/manual/en/function.wordwrap.php

« previous php.notes (#25693) next »