Plugin Directory

Changeset 200515


Ignore:
Timestamp:
02/01/2010 03:58:00 PM (16 years ago)
Author:
Mamoun.othman
Message:
 
File:
1 edited

Legend:

Unmodified
Added
Removed
  • featured-post-mu/trunk/featured-posts-mu.php

    r200227 r200515  
    2424    along with this program;
    2525*/
    26 
    27 // Main function to diplay on front end
     26function get_user_details_by_id( $id ) {
     27    global $wpdb;
     28    return $wpdb->get_row( $wpdb->prepare("SELECT user_login FROM $wpdb->users WHERE ID = %d", $id) );
     29}
    2830
    2931function ftable_exists($tablename, $database = false) {
     
    282284add_action ('deleted_post', 'fpl_delete_featured');
    283285
     286
     287function truncate($text, $length = 100, $ending = '...', $exact = true, $considerHtml = false) {
     288    if ($considerHtml) {
     289        // if the plain text is shorter than the maximum length, return the whole text
     290        if (strlen(preg_replace('/<.*?>/', '', $text)) <= $length) {
     291            return $text;
     292        }
     293        // splits all html-tags to scanable lines
     294        preg_match_all('/(<.+?>)?([^<>]*)/s', $text, $lines, PREG_SET_ORDER);
     295        $total_length = strlen($ending);
     296        $open_tags = array();
     297        $truncate = '';
     298        foreach ($lines as $line_matchings) {
     299            // if there is any html-tag in this line, handle it and add it (uncounted) to the output
     300            if (!empty($line_matchings[1])) {
     301                // if it's an "empty element" with or without xhtml-conform closing slash (f.e. <br/>)
     302                if (preg_match('/^<(\s*.+?\/\s*|\s*(img|br|input|hr|area|base|basefont|col|frame|isindex|link|meta|param)(\s.+?)?)>$/is', $line_matchings[1])) {
     303                    // do nothing
     304                // if tag is a closing tag (f.e. </b>)
     305                } else if (preg_match('/^<\s*\/([^\s]+?)\s*>$/s', $line_matchings[1], $tag_matchings)) {
     306                    // delete tag from $open_tags list
     307                    $pos = array_search($tag_matchings[1], $open_tags);
     308                    if ($pos !== false) {
     309                        unset($open_tags[$pos]);
     310                    }
     311                // if tag is an opening tag (f.e. <b>)
     312                } else if (preg_match('/^<\s*([^\s>!]+).*?>$/s', $line_matchings[1], $tag_matchings)) {
     313                    // add tag to the beginning of $open_tags list
     314                    array_unshift($open_tags, strtolower($tag_matchings[1]));
     315                }
     316                // add html-tag to $truncate'd text
     317                $truncate .= $line_matchings[1];
     318            }
     319            // calculate the length of the plain text part of the line; handle entities as one character
     320            $content_length = strlen(preg_replace('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', ' ', $line_matchings[2]));
     321            if ($total_length+$content_length> $length) {
     322                // the number of characters which are left
     323                $left = $length - $total_length;
     324                $entities_length = 0;
     325                // search for html entities
     326                if (preg_match_all('/&[0-9a-z]{2,8};|&#[0-9]{1,7};|[0-9a-f]{1,6};/i', $line_matchings[2], $entities, PREG_OFFSET_CAPTURE)) {
     327                    // calculate the real length of all entities in the legal range
     328                    foreach ($entities[0] as $entity) {
     329                        if ($entity[1]+1-$entities_length <= $left) {
     330                            $left--;
     331                            $entities_length += strlen($entity[0]);
     332                        } else {
     333                            // no more characters left
     334                            break;
     335                        }
     336                    }
     337                }
     338                $truncate .= substr($line_matchings[2], 0, $left+$entities_length);
     339                // maximum lenght is reached, so get off the loop
     340                break;
     341            } else {
     342                $truncate .= $line_matchings[2];
     343                $total_length += $content_length;
     344            }
     345            // if the maximum length is reached, get off the loop
     346            if($total_length>= $length) {
     347                break;
     348            }
     349        }
     350    } else {
     351        if (strlen($text) <= $length) {
     352            return $text;
     353        } else {
     354            $truncate = substr($text, 0, $length - strlen($ending));
     355        }
     356    }
     357    // if the words shouldn't be cut in the middle...
     358    if (!$exact) {
     359        // ...search the last occurance of a space...
     360        $spacepos = strrpos($truncate, ' ');
     361        if (isset($spacepos)) {
     362            // ...and cut the text in this position
     363            $truncate = substr($truncate, 0, $spacepos);
     364        }
     365    }
     366    // add the defined ending to the text
     367    $truncate .= $ending;
     368    if($considerHtml) {
     369        // close all unclosed html-tags
     370        foreach ($open_tags as $tag) {
     371            $truncate .= '</' . $tag . '>';
     372        }
     373    }
     374    return $truncate;
     375}
    284376?>
Note: See TracChangeset for help on using the changeset viewer.