Return to Snippet

Revision: 65320
at November 19, 2013 05:36 by JoshChris


Initial Code
/**
 * 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")));
}

Initial URL

                                

Initial Description
Truncates the given string at the specified length.

Initial Title
PHP: Truncate a string at a word break

Initial Tags
php

Initial Language
PHP