Trim Characters using PHP
by c.bavota | Posted in Tutorials | 12 comments
Aug
13
2012
I had previously written about how you can easily trim your text using a build in WordPress function called wp_trim_words() but sometimes you actually want to trim your text to a specific number of characters as opposed to actual words. Here is a short function that will do just that. If you want to use it in a WordPress theme, just place it within the PHP tags in your functions.php file.
/**
* Trims a string of words to a specified number of characters, gracefully stopping at white spaces.
* Also strips HTML tags, to prevent breaking in the middle of a tag.
*
* @param string $text The string of words to be trimmed.
* @param int $length Maximum number of characters; defaults to 45.
* @param string $append String to append to end, when trimmed; defaults to ellipsis.
*
* @return String of words trimmed at specified character length.
*
* @author c.bavota
*/
function trim_characters( $text, $length = 45, $append = '…' ) {
$length = (int) $length;
$text = trim( strip_tags( $text ) );
if ( strlen( $text ) > $length ) {
$text = substr( $text, 0, $length + 1 );
$words = preg_split( "/[\s]| /", $text, -1, PREG_SPLIT_NO_EMPTY );
preg_match( "/[\s]| /", $text, $lastchar, 0, $length );
if ( empty( $lastchar ) )
array_pop( $words );
$text = implode( ' ', $words ) . $append;
}
return $text;
}
With the function in place, you can use something similar to the following to trim your text strings:
$string = "This is a string of text that I want to trim down to a specific number of characters."; trim_characters( $string ); // Output: This is a string of text that I want to trim...



I find myself using this type of function all the time in both WP and other PHP projects. Hugely useful, and I love that it is smart enough to split at a full word.
I like the way you’ve done this – trimming the string with substring, and then popping off the last word, assuming the last character is not a space. It seems more efficient than what I generally do (which would involve just dropping characters off the end until I find a space.
Very nice solution. I prefer Regex for things like that. What’s your opinion about the Performance?
Thanks for you good and short description.
I really need to learn php to up my game. Thanks for the simple tip.
Function you write very well, but it still cut the strings just in case I use ‘ ’ to represent a space.
$string = “This is a string of text that I want to trim down to a specific number of characters.”;
trim_characters( $string ); // Output: This is a string of text…
And the time to perform this function quite a lot, I have tried other ways to write the function and it’s pretty good fake address this problem:
/** * * @author thiet ke website (aloxo.net) */ function trim_words( $text, $length = 45, $more = '…' ) { $length = (int) $length; $text = trim( str_replace(' ', ' ', strip_tags( $text ) ) ); if ( isset($text{$length}) ) { $next = $text{++$length}; $text = substr( $text, 0, $length ); $words = explode( ' ', $text); ($next != ' ')?array_pop( $words ):''; $text = implode( ' ', $words ) . $more; } return $text; }When I checked, the obtained results are very positive:
I use ‘ ’ to represent a space;
I used to check the string is:
$string = “This is a string of text that I want to trim down to a specific number of characters.”;
I’m sorry! its true function is, in the comment above, I’ve forgotten not handle string ‘ ’ should be translated into a space in the comment that.
/** * * @author thiet ke website (aloxo.net) */ function trim_words( $text, $length = 45, $more = '…' ) { $length = (int) $length; $text = trim( str_replace(' ', ' ', strip_tags( $text ) ) ); if ( isset($text{$length}) ) { $next = $text{++$length}; $text = substr( $text, 0, $length ); $words = explode( ' ', $text); ($next != ' ')?array_pop( $words ):''; $text = implode( ' ', $words ) . $more; } return $text; }If you want to trim words and you are using WordPress you should read this: http://bavotasan.com/2012/trim-your-text-with-wp_trim_words-in-wordpress/
Hello very nice site!! Man .. Beautiful .. Amazing .. I will bookmark your site and take the feeds additionally?I am happy to find a lot of useful info here within the publish, we want work out extra strategies in this regard, thank you for sharing. . . . . .
Nice way to trim words.
Thanks tutorial, all hard operation with string use regex