Skip to main content

Posts

Showing posts from January, 2013

Break a string without losing html tags in PHP

< ? php /** * Using this function, we can break ( substr ) a string without losing html tags, * * @param $text *    String which is to be shortened. * * @param $length *    The length of the string . * * @param $ending *    The string that is to be appended after shortening.  Defaults to &hellip; * * @param boolean $exact *   If false, $text will not be cut mid-word * * @param boolean $considerHtml *   If true, HTML tags would be handled correctly * * * @return *     string Trimmed string.. */ Function _html_substr ($text, $length = 100, $ending = '...', $exact = false, $considerHtml = true) {     if ($considerHtml) {       // If the plain text is shorter than the maximum length, return the whole text       if (strlen ( preg_replace ( '/<.*?>/', '', $text)) <= $length) {         return $text;       }       // splits all html-tags to scanable lines       preg_match_all ( '/(<.+?>) ? ([^<> ] * )