PHP Snippet: Truncate a String at a Word Break

Truncates any given string at the specified length.

/**
 * Truncates the given string at the specified length.
 *
 * @param string $str The input string.
 * @param int $width The number of chars at which the string will be truncated.
 * @return string
 */
function truncate($str, $width) {
    return current(explode("\n", wordwrap($str, $width, "...\n")));
}