cmfcmf/column-width-calculator

Calculate "perfect" table column widths in PHP.

Maintainers

👁 cmfcmf

Package info

github.com/cmfcmf/ColumnWidthCalculator

pkg:composer/cmfcmf/column-width-calculator

Statistics

Installs: 1 124

Dependents: 0

Suggesters: 0

Stars: 5

Open Issues: 0

1.0.0 2014-06-25 18:43 UTC

Requires

  • php: >=5.3

Requires (Dev)

None

Suggests

None

Provides

None

Conflicts

None

Replaces

None

GPLv2 68052fc8d4bd21ee4d948a7b5f19042fcc185997

  • Christian Flach

tablecolumncalculatewidthauto-width

This package is auto-updated.

Last update: 2026-06-24 09:04:30 UTC


README

Calculate "perfect" table column widths in PHP, based on my question at StackOverflow and @watcher's answer.

How to use

$rows needs to be an array of rows. Each $row needs to be an array of cells. Each $cell needs to have the cell's content in it.

<?php
/**
 * @param array $rows An array of rows, where each row is an array of cells containing the cell content.
 * @param bool $html Whether or not the rows contain html content. This will call html_entity_decode.
 * @param bool $stripTags Whether or not to strip tags (only if $html is true).
 * @param int $minPercentage The minimum percentage each row must be wide.
 * @param null $customColumnFunction A custom function to transform a cell's value before it's length is measured.
 */
$columnWidthCalculator = new Cmfcmf\ColumnWidthCalculator($rows);
/**
 * Calculate the column widths.
 *
 * @return array
 *
 * Explanation of return array:
 * - $columnSizes[$colNumber]['percentage'] The calculated column width in percents.
 * - $columnSizes[$colNumber]['calc'] The calculated column width in letters.
 *
 * - $columnSizes[$colNumber]['max'] The maximum column width in letters.
 * - $columnSizes[$colNumber]['avg'] The average column width in letters.
 * - $columnSizes[$colNumber]['raw'] An array of all the column widths of this column in letters.
 * - $columnSizes[$colNumber]['stdd'] The calculated standard deviation in letters.
 *
 * INTERNAL
 * - $columnSizes[$colNumber]['cv'] The calculated standard deviation / the average column width in letters.
 * - $columnSizes[$colNumber]['stdd/max'] The calculated standard deviation / the maximum column width in letters.
 */
$columnSizes = $columnWidthCalculator->calculateWidths();

License

GPLv2, see the LICENSE file.