• Hey guys,

    I’m using a dynamic sidebar with several widgets and I would like to trim the title/links display length once they reach a certain number of characters. I’ve found a function that’ll probably do what I need but I’ve got no idea how to apply it properly.

    /**
    * Add this to your page:
    * <?php
    * include “shorten_a_text_string.php”;
    * echo ShortenText($text);
    * ?>
    * where $text is the text you want to shorten.
    *
    * Example
    * Test it using this in a PHP page:
    * <?php
    * include “shortentext.php”;
    * $text = “The rain in Spain falls mainly on the plain.”;
    * echo ShortenText($text);
    * ?>
    */

    <?php
    function ShortenText($text)
        {
            // Change to the number of characters you want to display
            $chars = 25;
            $text = $text." ";
            $text = substr($text,0,$chars);
            $text = substr($text,0,strrpos($text,' '));
            $text = $text."...";
            return $text;
        }
    ?>

    Hope you understand what I mean. Any help would be awesome, thanks.

Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Recent Posts – How to trim title length’ is closed to new replies.