note 96754 deleted from function.wordwrap by danbrown
| From: | danbrown@php.net | Date: | Mon, 15 Mar 2010 16:20:29 +0000 |
| Subject: | note 96754 deleted from function.wordwrap by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-167040@lists.php.net to get a copy of this message | ||
Note Submitter: Robbert
----
Sanneschaap posted a function to wrap a string based on the actual pixel length.
I simplified the function to minimize server load.
Note: this is based on Arial, using the most common characters. Not every font has the same
character widths, and different operating systems have different fonts.
Also, this becomes less accurate when the font size increases. Using a 10px-14px Arial/sans-serif
font, you'll be alright though.
Some language specific characters could be added to the function, but if the function can't
find the character it will default to the most common character width, so it's not required.
Arguments:
$strings = string or array of strings to be processed.
$limit = maximum width in pixels (must be a number only).
$fontsize = the used font size (must be a number only; default is 11).
$suffix = the suffix used if a string is too long (must be a string; default is '...').
Function:
<?php
function crop_string($strings, $limit, $fontsize = 11, $suffix = '...') {
if(is_string($strings)) $array[] = $strings; else $array = $strings;
if(is_array($array)) {
$c['@'] = 1;
$c['%'] = $c['W'] = $c['w'] = 0.909413854;
$c['A'] = $c['G'] = $c['M'] = $c['O'] = $c['Q']
= $c['V'] = $c['Y'] = $c['m'] = 0.728241563;
$c['&'] = $c['B'] = $c['C'] = $c['D'] =
$c['H'] = $c['K'] = $c['N'] = $c['R'] = $c['S'] =
$c['U'] = $c['X'] = $c['Z'] = 0.637655417;
$c['#'] = $c['$'] = $c['+'] = $c['0'] = $c['1']
= $c['2'] = $c['3'] = $c['4'] = $c['5'] = $c['6']
= $c['7'] = $c['8'] = $c['9'] = $c['<'] =
$c['='] = $c['>'] = $c['?'] = $c['E'] = $c['F']
= $c['L'] = $c['P'] = $c['T'] = $c['_'] = $c['a']
= $c['b'] = $c['c'] = $c['d'] = $c['e'] = $c['g']
= $c['h'] = $c['n'] = $c['o'] = $c['p'] = $c['q']
= $c['s'] = $c['u'] = $c['v'] = $c['x'] = $c['y']
= $c['z'] = $c['~'] = 0.547069272;
$c['J'] = $c['^'] = $c['k'] = 0.456483126;
$c['"'] = $c['('] = $c[')'] = $c['*'] =
$c['-'] = $c['`'] = $c['f'] = $c['r'] = $c['{'] =
$c['}'] = 0.36589698;
$c[' '] = $c[','] = $c['.'] = $c['/'] = $c[':']
= $c[';'] = $c['['] = $c['\\'] = $c[']'] = $c['t']
= 0.275310835;
$c['!'] = $c['\''] = $c['I'] = $c['i'] =
$c['j'] = $c['l'] = $c['|'] = 0.184724689;
$suffixlength = 0;
for($i = 0; $i < strlen($suffix); $i++) $suffixlength += ($c[$suffix[$i]] * $fontsize); //
Check suffix length
for($i = 0; $i < count($array); $i++) {
$result = 0;
for($j = 0; $j < strlen($array[$i]); $j++) {
$result += ($c[$array[$i][$j]] == true) ? ($c[$array[$i][$j]] * $fontsize) : (0.547069272 *
$fontsize);
if($result < ($limit - $suffixlength)) $note = $j-1; // Note: (limit-suffix length) reached
if($result > $limit) {
$array[$i] = substr($array[$i], 0, $note) . $suffix; // Back to note, add suffix, save new
string
$j = strlen($array[$i]); // Stop loop
}
}
}
return $array;
} else return false;
}
?>