Differences
This shows you the differences between two versions of the page.
| Both sides previous revision Previous revision | |||
|
backend-tech:php:favorite-php-snippets:itrunc [Jul 6, 2026 02:53 AM] 47.15.233.30 removed |
— (current) | ||
|---|---|---|---|
| Line 1: | Line 1: | ||
| - | = String Truncation - iTrunc() = | ||
| - | A great string truncation function, originally from [[http:// | ||
| - | |||
| - | |||
| - | <code php> | ||
| - | /** | ||
| - | * Truncates a string to a certain length at the most sensible point. | ||
| - | * First, if there' | ||
| - | * If there is no ' | ||
| - | * If the string is truncated, " ..." is appended. | ||
| - | * If the string is already shorter than $length, it is returned unchanged. | ||
| - | * | ||
| - | * @static | ||
| - | * @param string | ||
| - | * @param int length the maximum length the string should be truncated to | ||
| - | * @return string | ||
| - | */ | ||
| - | function _iTrunc($string, | ||
| - | if (strlen($string)< | ||
| - | return $string; | ||
| - | } | ||
| - | |||
| - | $pos = strrpos($string," | ||
| - | if ($pos> | ||
| - | $string = substr($string, | ||
| - | $pos = strrpos($string," | ||
| - | } | ||
| - | if ($pos> | ||
| - | return substr($string, | ||
| - | } | ||
| - | |||
| - | $pos = strrpos($string," | ||
| - | if ($pos> | ||
| - | $string = substr($string, | ||
| - | $pos = strrpos($string," | ||
| - | } | ||
| - | if ($pos> | ||
| - | return substr($string, | ||
| - | } | ||
| - | |||
| - | return substr($string, | ||
| - | |||
| - | } | ||
| - | </ | ||