Quick Search:
 PHP Code: Neatly trim a string to a set number of words Jump to:  
Category: >> PHP Code >> Neatly trim a string to a set number of words  

<< lastnext >>

Snippet Name: Neatly trim a string to a set number of words

Description: Trim a string along a word boundary that you specify, and add '...' to the end.

Also see:
» Format money as millions, billions, et...
» Truncate text while preserving HTML ta...
» Neatly trim a string to a set number o...
» Convert UK Dates To mySQL Format Dates
» Search array elements for a substring
» Add a period at the end of a string if...
» Count capital letters in a string
» Search All Words Of A String In mySQL
» Remove Extra Spaces From A String
» Calculate the difference between two t...
» Get Query String with Javascript
» RMAN: Format Directives
» USER: connection information
» USER: User Information 2
» USER Information
» SQL*Plus formatting
» Create string by formatting an amount ...
» Search PL/SQL for a string/ key value
» INSTR (InString)
» SUBSTR (SubString)
» Generate unique strings of random numb...
» Count words or terms
» String Functions: REVERSE
» String Functions: LENGTH
» String Functions: INSTR
» String Functions: CONCAT
» String Functions: CHAR
» String Functions: INITCAP
» String Functions: LOWER
» String Functions: UPPER

Comment: (none)

Language: PHP
Highlight Mode: PHP
Last Modified: May 27th, 2010

// Limit a string to a specified number of words and add '...' to end
FUNCTION neat_trim($str, $n, $delim='...') {	
	$len = STRLEN($str);
	IF ($len > $n) {
		PREG_MATCH('/(.{' . $n . '}.*?)\b/', $str, $matches);
		RETURN RTRIM($matches[1]) . $delim;
		RETURN $str;

   Home |    Search |    Code Library |    Sponsors |    Privacy |    Terms of Use |    Contact Us © 2003 - 2024 psoug.org