PHP code example of csoellinger / silverstripe-limit-characters-with-html

1. Go to this page and download the library: Download csoellinger/silverstripe-limit-characters-with-html library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

csoellinger / silverstripe-limit-characters-with-html example snippets


/**
 * 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.
 */

/**
 * 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.
 */

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



$htmlText = '<b>Hello World</b>';

// example 1
DBHTMLText::create('Test')
  ->setValue($htmlText)
  ->LimitCharactersWithHtml(9)
  ->Value() // Output: <b>Hello Wo…</b>

// example 2
DBHTMLText::create('Test')
  ->setValue($htmlText)
  ->LimitCharactersWithHtmlToClosestWord(9)
  ->Value() // Output: <b>Hello…</b>