Text Abbreviation Function in PHP

We'll need this function in almost all of our projects, here is the PHP Text Abbreviation Function that i have prepared

<?php

function shorten($text, $str = 10) {
    if (strlen($text) > $str) {
        if (function_exists("mb_substr")) $text = mb_substr($text, 0, $str, "UTF-8").'..';
        else $text = substr($text, 0, $str).'..';
    }
    return $text;
}

// use of
$text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua";
echo shorten($text , 20);

good works :D

Comments

There are no comments, make the firs comment