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)