-
Notifications
You must be signed in to change notification settings - Fork 11
/
function.str_chop.php
47 lines (40 loc) · 1.52 KB
/
function.str_chop.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
/**
* Chop a string into a smaller string.
*
* @author Aidan Lister <aidan@php.net>
* @version 1.1.0
* @link http://aidanlister.com/2004/04/creating-a-string-exerpt-elegantly/
* @param mixed $string The string you want to shorten
* @param int $length The length you want to shorten the string to
* @param bool $center If true, chop in the middle of the string
* @param mixed $append String appended if it is shortened
*/
function str_chop($string, $length = 60, $center = false, $append = null)
{
// Set the default append string
if ($append === null) {
$append = ($center === true) ? ' ... ' : ' ...';
}
// Get some measurements
$len_string = strlen($string);
$len_append = strlen($append);
// If the string is longer than the maximum length, we need to chop it
if ($len_string > $length) {
// Check if we want to chop it in half
if ($center === true) {
// Get the lengths of each segment
$len_start = $length / 2;
$len_end = $len_start - $len_append;
// Get each segment
$seg_start = substr($string, 0, $len_start);
$seg_end = substr($string, $len_string - $len_end, $len_end);
// Stick them together
$string = $seg_start . $append . $seg_end;
} else {
// Otherwise, just chop the end off
$string = substr($string, 0, $length - $len_append) . $append;
}
}
return $string;
}