|
| 1 | +<?php |
| 2 | +/* |
| 3 | +* Tiny Compress Images - WordPress plugin. |
| 4 | +* Copyright (C) 2015-2018 Tinify B.V. |
| 5 | +* |
| 6 | +* This program is free software; you can redistribute it and/or modify it |
| 7 | +* under the terms of the GNU General Public License as published by the Free |
| 8 | +* Software Foundation; either version 2 of the License, or (at your option) |
| 9 | +* any later version. |
| 10 | +* |
| 11 | +* This program is distributed in the hope that it will be useful, but WITHOUT |
| 12 | +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 14 | +* more details. |
| 15 | +* |
| 16 | +* You should have received a copy of the GNU General Public License along |
| 17 | +* with this program; if not, write to the Free Software Foundation, Inc., 51 |
| 18 | +* Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 19 | +*/ |
| 20 | + |
| 21 | +class Tiny_Helpers { |
| 22 | + |
| 23 | + /** |
| 24 | + * truncate_text will truncate a string to a given length. |
| 25 | + * When text is longer than the given length, the string will be truncated and |
| 26 | + * the last characters will be replaced with an ellipsis. |
| 27 | + * |
| 28 | + * We can use mb_strlen & mb_substr as WordPress provides a compat function for |
| 29 | + * it if mbstring php module is not installed. |
| 30 | + * |
| 31 | + * @param string $text the text |
| 32 | + * @param integer $length the maximum length of the string |
| 33 | + * @return string the truncated string |
| 34 | + */ |
| 35 | + public static function truncate_text( string $text, int $length ) { |
| 36 | + if ( mb_strlen( $text ) > $length ) { |
| 37 | + return mb_substr( $text, 0, $length - 3 ) . '...'; |
| 38 | + } |
| 39 | + return $text; |
| 40 | + } |
| 41 | +} |
0 commit comments