forked from muglug/psl
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtruncate.php
39 lines (33 loc) · 1.13 KB
/
truncate.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
<?php
declare(strict_types=1);
namespace Psl\Str;
use Psl;
use Psl\Internal;
use function mb_strimwidth;
/**
* Get truncated string with specified width.
*
* @param int $offset The start position offset. Number of
* characters from the beginning of string. (First character is 0)
* @param int $width the width of the desired trim
* @param string|null $trim_marker a string that is added to the end of string
* when string is truncated
*
* @throws Psl\Exception\InvariantViolationException If the offset is out-of-bounds.
* @throws Psl\Exception\InvariantViolationException If an invalid $encoding is provided.
*
* @return string The truncated string. If trim_marker is set,
* trim_marker is appended to the return value.
*
* @pure
*/
function truncate(
string $string,
int $offset,
int $width,
?string $trim_marker = null,
?string $encoding = null
): string {
$offset = Internal\validate_offset($offset, length($string, $encoding));
return mb_strimwidth($string, $offset, $width, $trim_marker ?? '', Internal\internal_encoding($encoding));
}