PluginProbe
wpDataTables – WordPress Data Table, Dynamic Tables & Table Charts Plugin / 6.5.1.7
wpDataTables – WordPress Data Table, Dynamic Tables & Table Charts Plugin v6.5.1.7
6.5.1.7 6.5.1.6 6.5.1.5 6.5.1.4 6.5.1.3 6.5.1.2 6.5.1.1 6.5.0.9 6.5.0.8 6.5.0.7 6.5.0.6 trunk 3.4.2.40 3.4.2.41 3.4.2.42 3.4.2.43 3.4.2.44 3.4.2.45 3.4.2.46 3.4.2.47 3.4.2.48 3.4.2.49 3.4.2.50 6.3.2 6.3.3.1 All 47 releases
wpdatatables / lib / phpoffice / phpspreadsheet / src / PhpSpreadsheet / Calculation / TextData / Replace.php

Replace.php in wpDataTables – WordPress Data Table, Dynamic Tables & Table Charts Plugin 6.5.1.7, at lib/phpoffice/phpspreadsheet/src/PhpSpreadsheet/Calculation/TextData/Replace.php

119 lines 4.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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\Functions;
8 use PhpOffice\PhpSpreadsheet\Calculation\Information\ExcelError;
9 use PhpOffice\PhpSpreadsheet\Cell\DataType;
10 use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
11
12 class Replace
13 {
14 use ArrayEnabled;
15
16 /**
17 * REPLACE.
18 *
19 * @param mixed $oldText The text string value to modify
20 * Or can be an array of values
21 * @param mixed $start Integer offset for start character of the replacement
22 * Or can be an array of values
23 * @param mixed $chars Integer number of characters to replace from the start offset
24 * Or can be an array of values
25 * @param mixed $newText String to replace in the defined position
26 * Or can be an array of values
27 *
28 * @return array|string
29 * If an array of values is passed for either of the arguments, then the returned result
30 * will also be an array with matching dimensions
31 */
32 public static function replace($oldText, $start, $chars, $newText)
33 {
34 if (is_array($oldText) || is_array($start) || is_array($chars) || is_array($newText)) {
35 return self::evaluateArrayArguments([self::class, __FUNCTION__], $oldText, $start, $chars, $newText);
36 }
37
38 try {
39 $start = Helpers::extractInt($start, 1, 0, true);
40 $chars = Helpers::extractInt($chars, 0, 0, true);
41 $oldText = Helpers::extractString($oldText, true);
42 $newText = Helpers::extractString($newText, true);
43 $left = StringHelper::substring($oldText, 0, $start - 1);
44
45 $right = StringHelper::substring($oldText, $start + $chars - 1, null);
46 } catch (CalcExp $e) {
47 return $e->getMessage();
48 }
49 $returnValue = $left . $newText . $right;
50 if (StringHelper::countCharacters($returnValue) > DataType::MAX_STRING_LENGTH) {
51 $returnValue = ExcelError::VALUE();
52 }
53
54 return $returnValue;
55 }
56
57 /**
58 * SUBSTITUTE.
59 *
60 * @param mixed $text The text string value to modify
61 * Or can be an array of values
62 * @param mixed $fromText The string value that we want to replace in $text
63 * Or can be an array of values
64 * @param mixed $toText The string value that we want to replace with in $text
65 * Or can be an array of values
66 * @param mixed $instance Integer instance Number for the occurrence of frmText to change
67 * Or can be an array of values
68 *
69 * @return array|string
70 * If an array of values is passed for either of the arguments, then the returned result
71 * will also be an array with matching dimensions
72 */
73 public static function substitute($text = '', $fromText = '', $toText = '', $instance = null)
74 {
75 if (is_array($text) || is_array($fromText) || is_array($toText) || is_array($instance)) {
76 return self::evaluateArrayArguments([self::class, __FUNCTION__], $text, $fromText, $toText, $instance);
77 }
78
79 try {
80 $text = Helpers::extractString($text, true);
81 $fromText = Helpers::extractString($fromText, true);
82 $toText = Helpers::extractString($toText, true);
83 if ($instance === null) {
84 $returnValue = str_replace($fromText, $toText, $text);
85 } else {
86 if (is_bool($instance)) {
87 if ($instance === false || Functions::getCompatibilityMode() !== Functions::COMPATIBILITY_OPENOFFICE) {
88 return ExcelError::Value();
89 }
90 $instance = 1;
91 }
92 $instance = Helpers::extractInt($instance, 1, 0, true);
93 $returnValue = self::executeSubstitution($text, $fromText, $toText, $instance);
94 }
95 } catch (CalcExp $e) {
96 return $e->getMessage();
97 }
98 if (StringHelper::countCharacters($returnValue) > DataType::MAX_STRING_LENGTH) {
99 $returnValue = ExcelError::VALUE();
100 }
101
102 return $returnValue;
103 }
104
105 private static function executeSubstitution(string $text, string $fromText, string $toText, int $instance): string
106 {
107 $pos = -1;
108 while ($instance > 0) {
109 $pos = mb_strpos($text, $fromText, $pos + 1, 'UTF-8');
110 if ($pos === false) {
111 return $text;
112 }
113 --$instance;
114 }
115
116 return Functions::scalar(self::REPLACE($text, ++$pos, StringHelper::countCharacters($fromText), $toText));
117 }
118 }
119