Truncator
in package
This file is part of https://github.com/Bluetel-Solutions/twig-truncate-extension
Copyright (c) 2015 Bluetel Solutions developers@bluetel.co.uk Copyright (c) 2015 Alex Wilson ajw@bluetel.co.uk
For the full copyright and license information, please view the LICENSE file that was distributed with this source code.
Table of Contents
Methods
- htmlToDomDocument() : DOMDocument
- Builds a DOMDocument object from a string containing HTML.
- truncate() : string
- truncateLetters() : string
- Safely truncates HTML by a given number of letters.
- truncateWords() : string
- Safely truncates HTML by a given number of words.
- getCleanedHTML() : string
- Clean extra code
- insertEllipsis() : void
- Inserts an ellipsis
- removeProceedingNodes() : void
- Removes all nodes after the current node.
Methods
htmlToDomDocument()
Builds a DOMDocument object from a string containing HTML.
public
static htmlToDomDocument(string $html) : DOMDocument
Parameters
- $html : string
-
HTML to load
Return values
DOMDocument —Returns a DOMDocument object.
truncate()
public
truncate(string $text[, int $length = 100 ][, string $ending = '...' ][, bool $exact = false ][, bool $considerHtml = true ]) : string
Parameters
- $text : string
- $length : int = 100
- $ending : string = '...'
- $exact : bool = false
- $considerHtml : bool = true
Return values
stringtruncateLetters()
Safely truncates HTML by a given number of letters.
public
static truncateLetters(string $html[, int $limit = 0 ][, string $ellipsis = '' ]) : string
Parameters
- $html : string
-
Input HTML.
- $limit : int = 0
-
Limit to how many letters we preserve.
- $ellipsis : string = ''
-
String to use as ellipsis (if any).
Return values
string —Safe truncated HTML.
truncateWords()
Safely truncates HTML by a given number of words.
public
static truncateWords(string $html[, int $limit = 0 ][, string $ellipsis = '' ]) : string
Parameters
- $html : string
-
Input HTML.
- $limit : int = 0
-
Limit to how many words we preserve.
- $ellipsis : string = ''
-
String to use as ellipsis (if any).
Return values
string —Safe truncated HTML.
getCleanedHTML()
Clean extra code
private
static getCleanedHTML(DOMDocument $doc, DOMNode $container) : string
Parameters
- $doc : DOMDocument
- $container : DOMNode
Return values
stringinsertEllipsis()
Inserts an ellipsis
private
static insertEllipsis(DOMNode|DOMElement $domNode, string $ellipsis) : void
Parameters
- $domNode : DOMNode|DOMElement
-
Element to insert after.
- $ellipsis : string
-
Text used to suffix our document.
removeProceedingNodes()
Removes all nodes after the current node.
private
static removeProceedingNodes(DOMNode|DOMElement $domNode, DOMNode|DOMElement $topNode) : void
Parameters
- $domNode : DOMNode|DOMElement
- $topNode : DOMNode|DOMElement