complex-space / string-justifier
A PHP 5.3+ library for justifying strings.
v1.0.0
2024-06-07 23:15 UTC
Requires
- php: >=5.3
Suggests
- ext-mbstring: For best performance
- symfony/polyfill-mbstring: If you can't install ext-mbstring
This package is not auto-updated.
Last update: 2025-03-02 00:29:55 UTC
README
PHP 5.3+ library for justifying strings.
Usage
The constructor takes a line separator (used to split justified chunks), a word break character (used in the output when words are split) and a paragraph separator (separate paragraphs are treated individually, then joined together at the end).
$string = <<<EOF
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer et tempor velit, vitae porttitor mauris. Phasellus fermentum dignissim nulla quis eleifend. Praesent vestibulum diam nisi, non egestas enim aliquam sit amet. Etiam consequat ipsum risus. Duis quis urna velit. Vivamus condimentum bibendum felis sed tempor. Suspendisse consectetur nibh vel odio bibendum euismod. Cras vehicula aliquam leo, quis laoreet nunc tincidunt quis.
EOF;
$justifier = new \Space\Justifier\StringJustifier(
"\n", //line separator
"-", //indicate word break
"\n\n" //paragraph separator
);
echo $justifier->format(
$string,
32, //line width
3 //max allowed spaces between words
);
//gives the same output:
echo $justifier($string, 32, 3);
Unicode characters are properly handled. If the mb_* functions are not available then composer require symfony/polyfill-mbstring.