See Release Notes
Long Term Support Release
Differences Between: [Versions 400 and 401] [Versions 401 and 402] [Versions 401 and 403]
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 /** 106 * @return string 107 */ 108 private static function executeSubstitution(string $text, string $fromText, string $toText, int $instance) 109 { 110 $pos = -1; 111 while ($instance > 0) { 112 $pos = mb_strpos($text, $fromText, $pos + 1, 'UTF-8'); 113 if ($pos === false) { 114 break; 115 } 116 --$instance; 117 } 118 119 if ($pos !== false) { 120 return Functions::scalar(self::REPLACE($text, ++$pos, StringHelper::countCharacters($fromText), $toText)); 121 } 122 123 return $text; 124 } 125 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body