1441 |
ariadna |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace PhpOffice\PhpSpreadsheet\Calculation\TextData;
|
|
|
4 |
|
|
|
5 |
use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled;
|
|
|
6 |
use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcExp;
|
|
|
7 |
use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
|
|
|
8 |
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
|
|
|
9 |
|
|
|
10 |
class Search
|
|
|
11 |
{
|
|
|
12 |
use ArrayEnabled;
|
|
|
13 |
|
|
|
14 |
/**
|
|
|
15 |
* FIND (case sensitive search).
|
|
|
16 |
*
|
|
|
17 |
* @param mixed $needle The string to look for
|
|
|
18 |
* Or can be an array of values
|
|
|
19 |
* @param mixed $haystack The string in which to look
|
|
|
20 |
* Or can be an array of values
|
|
|
21 |
* @param mixed $offset Integer offset within $haystack to start searching from
|
|
|
22 |
* Or can be an array of values
|
|
|
23 |
*
|
|
|
24 |
* @return array|int|string The offset where the first occurrence of needle was found in the haystack
|
|
|
25 |
* If an array of values is passed for the $value or $chars arguments, then the returned result
|
|
|
26 |
* will also be an array with matching dimensions
|
|
|
27 |
*/
|
|
|
28 |
public static function sensitive(mixed $needle, mixed $haystack, mixed $offset = 1): array|string|int
|
|
|
29 |
{
|
|
|
30 |
if (is_array($needle) || is_array($haystack) || is_array($offset)) {
|
|
|
31 |
return self::evaluateArrayArguments([self::class, __FUNCTION__], $needle, $haystack, $offset);
|
|
|
32 |
}
|
|
|
33 |
|
|
|
34 |
try {
|
|
|
35 |
$needle = Helpers::extractString($needle, true);
|
|
|
36 |
$haystack = Helpers::extractString($haystack, true);
|
|
|
37 |
$offset = Helpers::extractInt($offset, 1, 0, true);
|
|
|
38 |
} catch (CalcExp $e) {
|
|
|
39 |
return $e->getMessage();
|
|
|
40 |
}
|
|
|
41 |
|
|
|
42 |
if (StringHelper::countCharacters($haystack) >= $offset) {
|
|
|
43 |
if (StringHelper::countCharacters($needle) === 0) {
|
|
|
44 |
return $offset;
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
$pos = mb_strpos($haystack, $needle, --$offset, 'UTF-8');
|
|
|
48 |
if ($pos !== false) {
|
|
|
49 |
return ++$pos;
|
|
|
50 |
}
|
|
|
51 |
}
|
|
|
52 |
|
|
|
53 |
return ExcelError::VALUE();
|
|
|
54 |
}
|
|
|
55 |
|
|
|
56 |
/**
|
|
|
57 |
* SEARCH (case insensitive search).
|
|
|
58 |
*
|
|
|
59 |
* @param mixed $needle The string to look for
|
|
|
60 |
* Or can be an array of values
|
|
|
61 |
* @param mixed $haystack The string in which to look
|
|
|
62 |
* Or can be an array of values
|
|
|
63 |
* @param mixed $offset Integer offset within $haystack to start searching from
|
|
|
64 |
* Or can be an array of values
|
|
|
65 |
*
|
|
|
66 |
* @return array|int|string The offset where the first occurrence of needle was found in the haystack
|
|
|
67 |
* If an array of values is passed for the $value or $chars arguments, then the returned result
|
|
|
68 |
* will also be an array with matching dimensions
|
|
|
69 |
*/
|
|
|
70 |
public static function insensitive(mixed $needle, mixed $haystack, mixed $offset = 1): array|string|int
|
|
|
71 |
{
|
|
|
72 |
if (is_array($needle) || is_array($haystack) || is_array($offset)) {
|
|
|
73 |
return self::evaluateArrayArguments([self::class, __FUNCTION__], $needle, $haystack, $offset);
|
|
|
74 |
}
|
|
|
75 |
|
|
|
76 |
try {
|
|
|
77 |
$needle = Helpers::extractString($needle, true);
|
|
|
78 |
$haystack = Helpers::extractString($haystack, true);
|
|
|
79 |
$offset = Helpers::extractInt($offset, 1, 0, true);
|
|
|
80 |
} catch (CalcExp $e) {
|
|
|
81 |
return $e->getMessage();
|
|
|
82 |
}
|
|
|
83 |
|
|
|
84 |
if (StringHelper::countCharacters($haystack) >= $offset) {
|
|
|
85 |
if (StringHelper::countCharacters($needle) === 0) {
|
|
|
86 |
return $offset;
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
$pos = mb_stripos($haystack, $needle, --$offset, 'UTF-8');
|
|
|
90 |
if ($pos !== false) {
|
|
|
91 |
return ++$pos;
|
|
|
92 |
}
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
return ExcelError::VALUE();
|
|
|
96 |
}
|
|
|
97 |
}
|