PHPackages                             csoellinger/silverstripe-limit-characters-with-html - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. csoellinger/silverstripe-limit-characters-with-html

ActiveSilverstripe-vendormodule[Utility &amp; Helpers](/categories/utility)

csoellinger/silverstripe-limit-characters-with-html
===================================================

A simple Silverstripe extension to limit DBHtmlText and DBHtmlVarChar without loosing html tags.

1.0.0(3y ago)2170↓50%[1 PRs](https://github.com/CSoellinger/silverstripe-limit-characters-with-html/pulls)MITPHPPHP ^7.4 || ^8.0 || ^8.1 || ^8.2

Since Mar 28Pushed 1y ago1 watchersCompare

[ Source](https://github.com/CSoellinger/silverstripe-limit-characters-with-html)[ Packagist](https://packagist.org/packages/csoellinger/silverstripe-limit-characters-with-html)[ RSS](/packages/csoellinger-silverstripe-limit-characters-with-html/feed)WikiDiscussions main Synced 1mo ago

READMEChangelogDependencies (5)Versions (4)Used By (0)

Limit Characters With HTML Extension
====================================

[](#limit-characters-with-html-extension)

This module adds an extension to DBHTMLVarchar and DBHTMLText which enables limit html text without loosing the html tags. For example if you want a teaser text written with a WYSIWYG.

1. [Requirements](#requirements)
2. [Installation](#installation)
3. [License](#license)
4. [Configuration](#configuration)
    1. [`private static bool html_min`](#private-static-bool-html_min)
    2. [`private static array html_min_options`](#private-static-array-html_min_options)
5. [Documentation](#documentation)
    1. [public function **LimitCharactersWithHtml(** $limit = 20, $add = false, $exact = true **)**: string](#public-function-limitcharacterswithhtml-limit--20-add--false-exact--true--string)
    2. [public function **LimitCharactersWithHtmlToClosestWord(** int $limit = 20, $add = false **)**: string](#public-function-limitcharacterswithhtmltoclosestword-int-limit--20-add--false--string)
    3. [public function **LongerThan(** int $limit, $excludeHtml = true **)**: bool](#public-function-longerthan-int-limit-excludehtml--true--bool)
6. [Usage examples](#usage-examples)
7. [Maintainers](#maintainers)
8. [Bugtracker](#bugtracker)
9. [Development and contribution](#development-and-contribution)

Requirements
------------

[](#requirements)

- Silverstripe ^4 | ^5

Installation
------------

[](#installation)

Simply install the extension with composer

```
composer require csoellinger/silverstripe-limit-characters-with-html
```

The extension will be auto added to [SilverStripe\\ORM\\FieldType\\DBHTMLVarchar](https://github.com/silverstripe/silverstripe-framework/blob/4/src/ORM/FieldType/DBHTMLVarchar.php) and [SilverStripe\\ORM\\FieldType\\DBHTMLText](https://github.com/silverstripe/silverstripe-framework/blob/4/src/ORM/FieldType/DBHTMLText.php) by the included config file. Just run:

```
sake dev/build "flush=all"
```

License
-------

[](#license)

See [License](License.md)

Configuration
-------------

[](#configuration)

### `private static bool html_min`

[](#private-static-bool-html_min)

Enable/Disable html minify.

### `private static array html_min_options`

[](#private-static-array-html_min_options)

Set HTML minify options as associative array:

- `collapse_whitespace` =&gt; true|false (Default: true)
- `disable_comments` =&gt; true|false (Default: false)

Documentation
-------------

[](#documentation)

After installing the module you have three new methods for your db html fields.

### public function **LimitCharactersWithHtml(** $limit = 20, $add = false, $exact = true **)**: string

[](#public-function-limitcharacterswithhtml-limit--20-add--false-exact--true--string)

```
/**
 * Limit this field's content by a number of characters. It can consider
 * html and limit exact or at word ending.
 *
 * @param int           $limit        Number of characters to limit by.
 * @param string|false  $add          Ellipsis to add to the end of truncated string.
 * @param bool          $exact        Truncate exactly or at word ending.
 *
 * @return string HTML text with limited characters.
 */
```

### public function **LimitCharactersWithHtmlToClosestWord(** int $limit = 20, $add = false **)**: string

[](#public-function-limitcharacterswithhtmltoclosestword-int-limit--20-add--false--string)

```
/**
 * Limit this field's content by a number of characters and truncate the field
 * to the closest complete word.
 *
 * @param int          $limit        Number of characters to limit by.
 * @param string|false $add          Ellipsis to add to the end of truncated string.
 *
 * @return string HTML text value with limited characters truncated to the closest word.
 */
```

### public function **LongerThan(** int $limit, $excludeHtml = true **)**: bool

[](#public-function-longerthan-int-limit-excludehtml--true--bool)

```
/**
 * Check if a string is longer than a number of characters. It excludes html
 * by default.
 *
 * @param mixed $excludeHtml Default is true
 *
 * @return bool
 */
```

Usage examples
--------------

[](#usage-examples)

```
