2017 © Pedro Peláez
 

library php-htmltruncator

HTML Truncation library, ported from the html_truncator rubygem

image

judev/php-htmltruncator

HTML Truncation library, ported from the html_truncator rubygem

  • Wednesday, May 9, 2018
  • by judev
  • Repository
  • 2 Watchers
  • 22 Stars
  • 21,035 Installations
  • PHP
  • 3 Dependents
  • 0 Suggesters
  • 6 Forks
  • 2 Open issues
  • 9 Versions
  • 3 % Grown

The README.md

PHP HTML Truncator

Build Status, (*1)

This is a PHP port of the html_truncator gem., (*2)

It will cleanly truncate HTML content, appending an ellipsis or other marker in the most suitable place. There is optional support for html5-php if parsing of non-well-formed HTML5 is required. This is because PHP's DOMDocument->loadHTML (or the underlying libxml) doesn't recognize HTML5., (*3)

During the truncation process the HTML will be loaded into a DOMDocument. This can be relatively slow and memory hungry, so you may want to cache the truncated output., (*4)

The intl and mbstring extensions are highly recommended to make truncation of international text more predictable., (*5)

How to use it

The HtmlTruncator\Truncator class has only one static method, truncate, with 3 arguments:, (*6)

  • the HTML-formatted string to truncate
  • the number of words to keep (real words, tags and attributes aren't counted)
  • some options like the ellipsis (optional, '…' by default). If the option is string it is used as the ellipsis

And a static attribute, $ellipsable_tags, which lists the tags that can contain the ellipsis (by default: p ol ul li div header article nav section footer aside dd dt dl)., (*7)

Examples

A simple example:, (*8)

Truncator::truncate("<p>Lorem ipsum dolor sit amet.</p>", 3);
# => "<p>Lorem ipsum dolor…</p>"

If the text is too short to be truncated, it won't be modified:, (*9)

Truncator::truncate("<p>Lorem ipsum dolor sit amet.</p>", 5);
# => "<p>Lorem ipsum dolor sit amet.</p>"

If you prefer, you can have the length in characters instead of words:, (*10)

Truncator::truncate("<p>Lorem ipsum dolor sit amet.</p>", 12, array('length_in_chars' => true));
# => "<p>Lorem ipsum …</p>"

You can customize the ellipsis:, (*11)

Truncator::truncate("<p>Lorem ipsum dolor sit amet.</p>", 3, " (truncated)");
# => "<p>Lorem ipsum dolor (truncated)</p>"

And even have HTML in the ellipsis:, (*12)

Truncator::truncate("<p>Lorem ipsum dolor sit amet.</p>", 3, '<a href="/more-to-read">...</a>');
# => "<p>Lorem ipsum dolor<a href="/more-to-read">...</a></p>"

The ellipsis is put at the right place, inside <p>, but not <i>:, (*13)

Truncator::truncate("<p><i>Lorem ipsum dolor sit amet.</i></p>", 3);
# => "<p><i>Lorem ipsum dolor</i>…</p>"

You can indicate that a tag can contain the ellipsis by adding it to the ellipsable_tags:, (*14)

Truncator::$ellipsable_tags[] = "blockquote";
Truncator::truncate("<blockquote>Lorem ipsum dolor sit amet.</blockquote>", 3);
# => "<blockquote>Lorem ipsum dolor…</blockquote>"

Credits

This PHP port is entirely based on Bruno Michel's excellent rubygem, any bugs introduced are likely mine., (*15)

Ported 2013-07-04 by Jude Venn judev@cuttlefish.com, released under the MIT license, (*16)

The Versions

09/05 2018

dev-master

9999999-dev https://github.com/judev/php-htmltruncator

HTML Truncation library, ported from the html_truncator rubygem

  Sources   Download

MIT

The Requires

 

The Development Requires

php html

06/04 2016

1.1.0

1.1.0.0 https://github.com/judev/php-htmltruncator

HTML Truncation library, ported from the html_truncator rubygem

  Sources   Download

MIT

The Requires

 

The Development Requires

php html

26/03 2015

1.0.6

1.0.6.0 https://github.com/judev/php-htmltruncator

HTML Truncation library, ported from the html_truncator rubygem

  Sources   Download

MIT

The Requires

 

The Development Requires

php html

26/03 2015

1.0.5

1.0.5.0 https://github.com/judev/php-htmltruncator

HTML Truncation library, ported from the html_truncator rubygem

  Sources   Download

MIT

The Requires

 

The Development Requires

php html

26/03 2015

1.0.4

1.0.4.0 https://github.com/judev/php-htmltruncator

HTML Truncation library, ported from the html_truncator rubygem

  Sources   Download

MIT

The Requires

 

The Development Requires

php html

23/03 2015

1.0.3

1.0.3.0 https://github.com/judev/php-htmltruncator

HTML Truncation library, ported from the html_truncator rubygem

  Sources   Download

MIT

The Requires

 

The Development Requires

php html

11/03 2015

1.0.2

1.0.2.0 https://github.com/judev/php-htmltruncator

HTML Truncation library, ported from the html_truncator rubygem

  Sources   Download

MIT

The Requires

 

The Development Requires

php html

04/03 2015

1.0.1

1.0.1.0 https://github.com/judev/php-htmltruncator

HTML Truncation library, ported from the html_truncator rubygem

  Sources   Download

MIT

The Requires

 

The Development Requires

php html

03/03 2014

1.0.0

1.0.0.0 https://github.com/judev/php-htmltruncator

HTML Truncation library, ported from the html_truncator rubygem

  Sources   Download

MIT

The Requires

 

php html