Skip to content

kayw-geek/php-wordwrap

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

PHP-WordWrap

GitHubphp-standard-styleGitHub top language

Word wrapping, with a few features.

  • force-break option
  • wraps hypenated words
  • multilingual - wraps any language that uses whitespace for word separation.
  • custom symbol wrap mode
  • chain call
  • multi-format return

Install

composer require kayw-geek/php-wordwrap

Synopsis

Wrap some text in a 20 character column.

> $wrap = new \KaywGeek\WordWrap();

> $text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.";

> $result = $wrap->text($text)->width(20)->wrap();

result now looks like this:

Lorem ipsum dolor
sit amet,
consectetur
adipiscing elit, sed
do eiusmod tempor
incididunt ut labore
et dolore magna
aliqua.

By default, long words will not break. Unless you set the break option.

> $text = "https://github.com/kayw-geek/php-wordwrap"

> $wrap->text($text)->width(28)->break(false)->wrap();
  https://github.com/kayw-geek/php-wordwrap

> $wrap->text($text)->width(28)->break()->wrap();
  https://github.com/kayw-geek
  /php-wordwrap

Punctuation wrap mode

> $text = "Of course,the first example appears to be the nicest one (or perhaps the fourth),but you may find that being able to use empty expressions in for loops comes in handy in many occasions.";


> $wrap->text($text)->lfEnable()->wrap();

result

Of course,
the first example appears to be the nicest one (or perhaps the fourth),
but you may find that being able to use empty expressions in for loops comes in handy in many occasions.

Format data

> $text = "Of course,the first example appears to be the nicest one (or perhaps the fourth),but you may find that being able to use empty expressions in for loops comes in handy in many occasions.";

/**
* Format List
* \KaywGeek\WordWrap::FORMAT_JSON
* \KaywGeek\WordWrap::FORMAT_STRING
* \KaywGeek\WordWrap::FORMAT_ARRAY
*/
> $wrap->text($text)
    ->lfEnable()
    ->responseFormat(\KaywGeek\WordWrap::FORMAT_JSON)
    ->wrap();

Inspiration

wordwrapjs