note 82580 added to function.wordwrap

From: Date: Thu, 17 Apr 2008 08:41:48 +0000
Subject: note 82580 added to function.wordwrap
Groups: php.notes 
Request: Send a blank email to php-notes+get-138394@lists.php.net to get a copy of this message
These functions let you wrap strings comparing to their actual displaying width of proportional font. In this case Arial, 11px. Very handy in some cases since CSS3 is not yet completely supported. 100 strings = ~5 ms My old sheep word wrap function (posted at the bottom of this page, is kinda old dated and this one is faster and more accurate). //the width of the biggest char @ $fontwidth = 11; //each chargroup has char-ords that have the same proportional displaying width $chargroup[0] = array(64); $chargroup[1] = array(37,87,119); $chargroup[2] = array(65,71,77,79,81,86,89,109); $chargroup[3] = array(38,66,67,68,72,75,78,82,83,85,88,90); $chargroup[4] = array(35,36,43,48,49,50,51,52,53,54,55,56,57,60,61,62,63, 69,70,76,80,84,95,97,98,99,100,101,103,104,110,111,112, 113,115,117,118,120,121,122,126); $chargroup[5] = array(74,94,107); $chargroup[6] = array(34,40,41,42,45,96,102,114,123,125); $chargroup[7] = array(44,46,47,58,59,91,92,93,116); $chargroup[8] = array(33,39,73,105,106,108,124); //how the displaying width are compared to the biggest char width $chargroup_relwidth[0] = 1; //is char @ $chargroup_relwidth[1] = 0.909413854; $chargroup_relwidth[2] = 0.728241563; $chargroup_relwidth[3] = 0.637655417; $chargroup_relwidth[4] = 0.547069272; $chargroup_relwidth[5] = 0.456483126; $chargroup_relwidth[6] = 0.36589698; $chargroup_relwidth[7] = 0.275310835; $chargroup_relwidth[8] = 0.184724689; //build fast array $char_relwidth = null; for ($i=0;$i<count($chargroup);$i++){ for ($j=0;$j<count($chargroup[$i]);$j++){ $char_relwidth[$chargroup[$i][$j]] = $chargroup_relwidth[$i]; } } //get the display width (in pixels) of a string function get_str_width($str){ global $fontwidth,$char_relwidth; $result = 0; for ($i=0;$i<strlen($str);$i++){ $result += $char_relwidth[ord($str[$i])]; } $result = $result * $fontwidth; return $result; } //truncates a string at a certain displaying pixel width function truncate_str_at_width($str, $width, $trunstr='...'){ global $fontwidth,$char_relwidth; $trunstr_width = get_str_width($trunstr); $width -= $trunstr_width; $width = $width/$fontwidth; $w = 0; for ($i=0;$i<strlen($str);$i++){ $w += $char_relwidth[ord($str[$i])]; if ($w > $width) break; } $result = substr($str,0,$i).$trunstr; return $result; // texas is the reason rules at 10am :) } ---- Server IP: 208.69.120.35 Probable Submitter: 217.196.45.116 ---- Manual Page -- http://www.php.net/manual/en/function.wordwrap.php Edit -- https://master.php.net/note/edit/82580 Del: integrated -- https://master.php.net/note/delete/82580/integrated Del: useless -- https://master.php.net/note/delete/82580/useless Del: bad code -- https://master.php.net/note/delete/82580/bad+code Del: spam -- https://master.php.net/note/delete/82580/spam Del: non-english -- https://master.php.net/note/delete/82580/non-english Del: in docs -- https://master.php.net/note/delete/82580/in+docs Del: other reasons-- https://master.php.net/note/delete/82580 Reject -- https://master.php.net/note/reject/82580 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#138394) next »