|
3 | 3 | namespace PhpOffice\PhpSpreadsheet\Calculation\TextData; |
4 | 4 |
|
5 | 5 | use PhpOffice\PhpSpreadsheet\Calculation\ArrayEnabled; |
| 6 | +use PhpOffice\PhpSpreadsheet\Calculation\Functions; |
6 | 7 |
|
7 | 8 | class Text |
8 | 9 | { |
@@ -77,4 +78,133 @@ public static function test($testValue = '') |
77 | 78 |
|
78 | 79 | return null; |
79 | 80 | } |
| 81 | + |
| 82 | + /** |
| 83 | + * TEXTSPLIT. |
| 84 | + * |
| 85 | + * @param mixed $text the text that you're searching |
| 86 | + * @param null|array|string $columnDelimiter The text that marks the point where to spill the text across columns. |
| 87 | + * Multiple delimiters can be passed as an array of string values |
| 88 | + * @param null|array|string $rowDelimiter The text that marks the point where to spill the text down rows. |
| 89 | + * Multiple delimiters can be passed as an array of string values |
| 90 | + * @param bool $ignoreEmpty Specify FALSE to create an empty cell when two delimiters are consecutive. |
| 91 | + * true = create empty cells |
| 92 | + * false = skip empty cells |
| 93 | + * Defaults to TRUE, which creates an empty cell |
| 94 | + * @param bool $matchMode Determines whether the match is case-sensitive or not. |
| 95 | + * true = case-sensitive |
| 96 | + * false = case-insensitive |
| 97 | + * By default, a case-sensitive match is done. |
| 98 | + * @param mixed $padding The value with which to pad the result. |
| 99 | + * The default is #N/A. |
| 100 | + * |
| 101 | + * @return array the array built from the text, split by the row and column delimiters |
| 102 | + */ |
| 103 | + public static function split($text, $columnDelimiter = null, $rowDelimiter = null, bool $ignoreEmpty = false, bool $matchMode = true, $padding = '#N/A') |
| 104 | + { |
| 105 | + $text = Functions::flattenSingleValue($text); |
| 106 | + |
| 107 | + $flags = self::matchFlags($matchMode); |
| 108 | + |
| 109 | + if ($rowDelimiter !== null) { |
| 110 | + $delimiter = self::buildDelimiter($rowDelimiter); |
| 111 | + $rows = ($delimiter === '()') |
| 112 | + ? [$text] |
| 113 | + : preg_split("/{$delimiter}/{$flags}", $text); |
| 114 | + } else { |
| 115 | + $rows = [$text]; |
| 116 | + } |
| 117 | + |
| 118 | + /** @var array $rows */ |
| 119 | + if ($ignoreEmpty === true) { |
| 120 | + $rows = array_values(array_filter( |
| 121 | + $rows, |
| 122 | + function ($row) { |
| 123 | + return $row !== ''; |
| 124 | + } |
| 125 | + )); |
| 126 | + } |
| 127 | + |
| 128 | + if ($columnDelimiter !== null) { |
| 129 | + $delimiter = self::buildDelimiter($columnDelimiter); |
| 130 | + array_walk( |
| 131 | + $rows, |
| 132 | + function (&$row) use ($delimiter, $flags, $ignoreEmpty): void { |
| 133 | + $row = ($delimiter === '()') |
| 134 | + ? [$row] |
| 135 | + : preg_split("/{$delimiter}/{$flags}", $row); |
| 136 | + /** @var array $row */ |
| 137 | + if ($ignoreEmpty === true) { |
| 138 | + $row = array_values(array_filter( |
| 139 | + $row, |
| 140 | + function ($value) { |
| 141 | + return $value !== ''; |
| 142 | + } |
| 143 | + )); |
| 144 | + } |
| 145 | + } |
| 146 | + ); |
| 147 | + if ($ignoreEmpty === true) { |
| 148 | + $rows = array_values(array_filter( |
| 149 | + $rows, |
| 150 | + function ($row) { |
| 151 | + return $row !== [] && $row !== ['']; |
| 152 | + } |
| 153 | + )); |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + return self::applyPadding($rows, $padding); |
| 158 | + } |
| 159 | + |
| 160 | + /** |
| 161 | + * @param mixed $padding |
| 162 | + */ |
| 163 | + private static function applyPadding(array $rows, $padding): array |
| 164 | + { |
| 165 | + $columnCount = array_reduce( |
| 166 | + $rows, |
| 167 | + function (int $counter, array $row): int { |
| 168 | + return max($counter, count($row)); |
| 169 | + }, |
| 170 | + 0 |
| 171 | + ); |
| 172 | + |
| 173 | + return array_map( |
| 174 | + function (array $row) use ($columnCount, $padding): array { |
| 175 | + return (count($row) < $columnCount) |
| 176 | + ? array_merge($row, array_fill(0, $columnCount - count($row), $padding)) |
| 177 | + : $row; |
| 178 | + }, |
| 179 | + $rows |
| 180 | + ); |
| 181 | + } |
| 182 | + |
| 183 | + /** |
| 184 | + * @param null|array|string $delimiter the text that marks the point before which you want to split |
| 185 | + * Multiple delimiters can be passed as an array of string values |
| 186 | + */ |
| 187 | + private static function buildDelimiter($delimiter): string |
| 188 | + { |
| 189 | + $valueSet = Functions::flattenArray($delimiter); |
| 190 | + |
| 191 | + if (is_array($delimiter) && count($valueSet) > 1) { |
| 192 | + $quotedDelimiters = array_map( |
| 193 | + function ($delimiter) { |
| 194 | + return preg_quote($delimiter ?? ''); |
| 195 | + }, |
| 196 | + $valueSet |
| 197 | + ); |
| 198 | + $delimiters = implode('|', $quotedDelimiters); |
| 199 | + |
| 200 | + return '(' . $delimiters . ')'; |
| 201 | + } |
| 202 | + |
| 203 | + return '(' . preg_quote(Functions::flattenSingleValue($delimiter)) . ')'; |
| 204 | + } |
| 205 | + |
| 206 | + private static function matchFlags(bool $matchMode): string |
| 207 | + { |
| 208 | + return ($matchMode === true) ? 'miu' : 'mu'; |
| 209 | + } |
80 | 210 | } |
0 commit comments