public static function stringLimit($string, $options = null)
    {
        $options = Helper::getOptions($options, [
            "length"    =>  30,
            "extra"     =>  0,
            "alt"       =>  "n/a",
            "words"     =>  true,
            "suffix"    =>  "...",
        ]);

        if (!$string = trim($string)) {
            return $options["alt"];
        }

        if ($options["words"]) {
            while (mb_strlen($string) > ($options["length"] + $options["extra"])) {
                $string = mb_substr($string, 0, $options["length"]);
                $string = trim($string);

                $words = explode(" ", $string);
                array_pop($words);
                $string = implode(" ", $words);

                $string .= $options["suffix"];
            }
        } else {
            $length = $options["length"] + $options["extra"] + mb_strlen($options["suffix"]);
            if (mb_strlen($string) > $length) {
                $string = mb_substr($string, 0, $length);
                $string .= $options["suffix"];
            }
        }

        return $string;
    }
public static function stringLimit($string, $options = null)
    {
        $options = Helper::getOptions($options, [
            "length"    =>  30,
            "extra"     =>  0,
            "alt"       =>  "n/a",
            "words"     =>  true,
            "suffix"    =>  "...",
        ]);

        if (!$string = trim($string)) {
            return $options["alt"];
        }

        if ($options["words"]) {
            while (mb_strlen($string) > ($options["length"] + $options["extra"])) {
                $string = mb_substr($string, 0, $options["length"]);
                $string = trim($string);

                $words = explode(" ", $string);
                array_pop($words);
                $string = implode(" ", $words);

                $string .= $options["suffix"];
            }
        } else {
            $length = $options["length"] + $options["extra"] + mb_strlen($options["suffix"]);
            if (mb_strlen($string) > $length) {
                $string = mb_substr($string, 0, $length);
                $string .= $options["suffix"];
            }
        }

        return $string;
    }
public static function stringLimit ( $ string , $ options = null ) { $ options = Helper :: getOptions ( $ options , [ "length" => 30 , "extra" => 0 , "alt" => "n/a" , "words" => true , "suffix" => "..." , ] ) ; if ( ! $ string = trim ( $ string ) ) { return $ options [ "alt" ] ; } if ( $ options [ "words" ] ) { while ( mb_strlen ( $ string ) > ( $ options [ "length" ] + $ options [ "extra" ] ) ) { $ string = mb_substr ( $ string , 0 , $ options [ "length" ] ) ; $ string = trim ( $ string ) ; $ words = explode ( " " , $ string ) ; array_pop ( $ words ) ; $ string = implode ( " " , $ words ) ; $ string .= $ options [ "suffix" ] ; } } else { $ length = $ options [ "length" ] + $ options [ "extra" ] + mb_strlen ( $ options [ "suffix" ] ) ; if ( mb_strlen ( $ string ) > $ length ) { $ string = mb_substr ( $ string , 0 , $ length ) ; $ string .= $ options [ "suffix" ] ; } } return $ string ; }
Limit a string to a maximum length.

$options:
- int "length" The maximum length that the string can be (default: 30)
- int "extra" The extra length that is acceptable, while aiming for the above length (default: 0)
- string "alt" The string that should be returned if the input is falsey (default: "n/a")
- bool "words" Set to true to never break in the middle of a word (default: true)
- string "suffix" The string that should be appended if the input has been shortened (default: "...")

@param string $string The string to limit
@param array $options An array of options (see above)

@return string
Limit a string to a maximum length .