Pages

Thursday, April 3, 2014

Split to max characters without breaking word - PHP [resolved]

<?php

function split_str($str, $maxlen) {
    if (strlen($str) <= $maxlen) {
       return $str;
    } 
    $newstr = substr($str, 0, $maxlen);
    if ( substr($newstr,-1,1) != ' ' )
        $newstr = substr($newstr, 0, strrpos($newstr, " "));
    return $newstr;
}

echo split_str("Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.", 22);

?>

No comments: