The example in this article describes how to truncate strings in JavaScript. Share it with everyone for your reference. The details are as follows:
Here JavaScript truncates the string, similar to substr(), except that this function does not truncate the word. After truncation occurs, an ellipsis will be added
if (!function_exists('subsent')) { function subsent($string, $start = 0, $length = 0, $cap = '...') { if ($length <= 0 || strlen($string) < $length) { return $string; } $string = substr($string, $start, strpos($string, ' ', $length)) . ' ' . $cap; return $string; } }
I hope this article will be helpful to everyone’s JavaScript programming design.