Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Truncate a given sting just after a fixed count of chars using PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 96
    Comment on it

    Hello Reader's! if you want to short a given string on fixed number of chars then php offers you many ways. Below is one of them, you can use this code for the same. Lets see the example below:-

    $sting_given  = "1234567890134667896abcdtiABCDEFGHIJKLMNOPQRSTUVWYXZ1234567890";
    $string_processed = "1234567890134667896abcdtiABCDEFGHIJK ";
    
    
    function truncate($string, $chars = 50, $terminator = ' ') {
        $cutPos = $chars - mb_strlen($terminator);
        $boundaryPos = mb_strrpos(mb_substr($string, 0, mb_strpos($string, ' ', $cutPos)), ' ');
        return mb_substr($string, 0, $boundaryPos === false ? $cutPos : $boundaryPos) . $terminator;
    }
    

    Now everytime the when the string more than 50 chars is given it will cut all the chars after 50 and replace then with (...)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: