<?php
$string  = "Crohn's disease is an autoimmune disease that affects thousands of people across America. Many studies were done regarding the rising Crohn's epidemic. These studies found the most incidences of Crohn's disease among the Jewish population..";
    $maxlength = 62;
    $extension = " ...";
    //echo strlen($string);
    //echo "<br/>";
    function truncate_string ($string, $maxlength, $extension) {
        // Set the replacement for the "string break" in the wordwrap function
        $cutmarker = "**cut_here**";
        // Checking if the given string is longer than $maxlength
        if (strlen($string) > $maxlength) {
            // Using wordwrap() to set the cutmarker
            // NOTE: wordwrap (PHP 4 >= 4.0.2, PHP 5)
            $string = wordwrap($string, $maxlength, $cutmarker);
            //echo $string."<br/>";
            // Exploding the string at the cutmarker, set by wordwrap()
            $string = explode($cutmarker, $string);
            //echo $string."<br/>";
            // Adding $extension to the first value of the array $string, returned by explode()
            $string = $string[0] . $extension;
            //echo $string."<br/>";
        }

        // returning $string
        return $string;
    }
    // This Will output:
    // "Truncates a string at a certain position without ?cutting? ..."
    
    echo truncate_string ($string, $maxlength, $extension);
    //echo "<br/>";
    //echo truncate_string ($namestring, $maxlength, $extension);
?>

Categories: Magento

0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *