Extract.php
3.5 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<?php
namespace PhpOffice\PhpSpreadsheet\Calculation\TextData;
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcExp;
class Extract
{
use ArrayEnabled;
/**
* LEFT.
*
* @param mixed $value String value from which to extract characters
* Or can be an array of values
* @param mixed $chars The number of characters to extract (as an integer)
* Or can be an array of values
*
* @return array|string The joined string
* If an array of values is passed for the $value or $chars arguments, then the returned result
* will also be an array with matching dimensions
*/
public static function left($value, $chars = 1)
{
if (is_array($value) || is_array($chars)) {
return self::evaluateArrayArguments([self::class, __FUNCTION__], $value, $chars);
}
try {
$value = Helpers::extractString($value);
$chars = Helpers::extractInt($chars, 0, 1);
} catch (CalcExp $e) {
return $e->getMessage();
}
return mb_substr($value ?? '', 0, $chars, 'UTF-8');
}
/**
* MID.
*
* @param mixed $value String value from which to extract characters
* Or can be an array of values
* @param mixed $start Integer offset of the first character that we want to extract
* Or can be an array of values
* @param mixed $chars The number of characters to extract (as an integer)
* Or can be an array of values
*
* @return array|string The joined string
* If an array of values is passed for the $value, $start or $chars arguments, then the returned result
* will also be an array with matching dimensions
*/
public static function mid($value, $start, $chars)
{
if (is_array($value) || is_array($start) || is_array($chars)) {
return self::evaluateArrayArguments([self::class, __FUNCTION__], $value, $start, $chars);
}
try {
$value = Helpers::extractString($value);
$start = Helpers::extractInt($start, 1);
$chars = Helpers::extractInt($chars, 0);
} catch (CalcExp $e) {
return $e->getMessage();
}
return mb_substr($value ?? '', --$start, $chars, 'UTF-8');
}
/**
* RIGHT.
*
* @param mixed $value String value from which to extract characters
* Or can be an array of values
* @param mixed $chars The number of characters to extract (as an integer)
* Or can be an array of values
*
* @return array|string The joined string
* If an array of values is passed for the $value or $chars arguments, then the returned result
* will also be an array with matching dimensions
*/
public static function right($value, $chars = 1)
{
if (is_array($value) || is_array($chars)) {
return self::evaluateArrayArguments([self::class, __FUNCTION__], $value, $chars);
}
try {
$value = Helpers::extractString($value);
$chars = Helpers::extractInt($chars, 0, 1);
} catch (CalcExp $e) {
return $e->getMessage();
}
return mb_substr($value ?? '', mb_strlen($value ?? '', 'UTF-8') - $chars, $chars, 'UTF-8');
}
}