Find out how many lines will text occupy





14
Date Submitted Wed. Mar. 22nd, 2006 6:10 PM
Revision 2 of 2
Beginner nev3rm0re
Tags PHP | String
Comments 1 comments
This function calculates how many lines will text occupy.
Example (simple):


$string = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Phasellus tincidunt posuere dolor";

$num_lines = how_many_lines(10, $string);

It accepts a third, optional, parameter $options, that allows you to change behaviour. $options should be array. Following configuration options are available:

'white_spaces' - default: array(' ', "\t") - array of chars, that should be treated as whitespace.

'new_lines' - default: array("\r\n", "\n") - array of strings, that should be treated as newlines. For example, for HTML you can set 'new_lines' => array('');

'force_line_breaks' - default: true - force wrapping, when the token is longer than width, or not. If set to false, and token cannot be fitted into $width, function will return false;

'callback' - default: null - callback function for determining character width. Must accept at least one parameter - $char

'callback_params' - default: array() - optional additional callback parameters

'char_widths' - default: null - associative array in a form $char => $width, which contains char width. If $char is not found in the array, it's width is defaulted to 0.

PHP

<?
function how_many_lines($width, $string, $options = array()) {
    if (!is_array($options)) $options = array();
    if ($width < 1) {
        trigger_error('Width should be > 0');
        return false;
    }
    $default_options = array(
    'white_spaces'      => array(' ', "\t"),
    'new_lines'  => array("\r\n", "\n"),
    'force_line_breaks' => true,
    'callback'    => null,
    'callback_params'   => array(),
    'char_widths'       => null,
    );

    extract(array_merge($default_options, $options));

    if ($callback == null) {
        if ($char_widths != null) {
            $callback = create_function('$char, $char_map', 'return (isset($char_map[$char]) ? $char_map[$char] : 0);');
            $callback_params = array($char_widths);
        } else {
            $callback = create_function('$char', 'return 1;');
        }
    }

    $string = str_replace($new_lines, "\n", $string);
    $string = str_replace($white_spaces, ' ', $string);

    $str_length = strlen($string);

    if ($str_length > 0 && $string[$str_length-1] == "\n") {
        $str_length--;
    }

    $sep = -1;
    $pos = $j = $line_length = 0;
    $number_of_lines = 1;

    while ($pos < $str_length) {
        $char = $string[$pos];
        if ($char == "\n") {
            $number_of_lines++;
            $pos++;
            $sep = -1;
            $j = $pos;
            $line_length = 0;
        } else {
            if ($char == ' ') {
                $sep = $pos;
            }

            $line_length += call_user_func_array($callback, array_merge(array($char), $callback_params));
            if ($line_length > $width) {
                if ($sep == -1) {
                    if (!$force_line_breaks) {
                        return false;
                    } else {
                        if ($pos == $j) {
                            $pos++;
                        }
                    }
                } else {
                    $pos = $sep + 1;
                }
                $sep = -1;
                $j = $pos;
                $line_length = 0;
                $number_of_lines++;
            } else {
                $pos++;
            }
        }
    }

    return $number_of_lines;
}
?>

 

German Rumm

Comments

Comments A simpler version?
Tue. Aug. 8th, 2006 7:52 PM    Beginner schmalls

Voting