Tuesday, May 17, 2011

Wrapper Function for String limit

Example #1

/**
 * function to return the part of the string if overflows
 */
function limit_text ($text, $count) {
  if (strlen($text) > $count) {
    return substr($text, 0, ($count-2)) . '...';
  }
  return $text;
}


Example #2

Truncate a UTF-8-encoded string safely to a number of characters.

Parameters

$string The string to truncate.
$len An upper limit on the returned string length.
$wordsafe Flag to truncate at last space within the upper limit. Defaults to FALSE.
$dots Flag to add trailing dots. Defaults to FALSE.

Return value

The truncated string.

Syntax:
truncate_utf8($string, $len, $wordsafe = FALSE, $dots = FALSE)

Example: 
echo truncate_utf8($continuumDetails['continuum_name'], 35, true, true);

No comments:

Post a Comment

Followers