Skip to content

Instantly share code, notes, and snippets.

View cmd05's full-sized avatar
:electron:
Experimenting

cmd05 cmd05

:electron:
Experimenting
  • undefined
View GitHub Profile
@TimBHowe
TimBHowe / gist:6765309
Created September 30, 2013 15:16
PHP function to truncate a string up to a number of characters while preserving whole words and HTML tags. Source from http://alanwhipple.com/2011/05/25/php-truncate-string-preserving-html-tags-words/
<?php
/**
* truncateHtml can truncate a string up to a number of characters while preserving whole words and HTML tags
*
* @param string $text String to truncate.
* @param integer $length Length of returned string, including ellipsis.
* @param string $ending Ending to be appended to the trimmed string.
* @param boolean $exact If false, $text will not be cut mid-word
* @param boolean $considerHtml If true, HTML tags would be handled correctly
*