Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.

Differences Between: [Versions 400 and 401] [Versions 400 and 402] [Versions 400 and 403]

   1  <?php
   2  
   3  namespace PhpOffice\PhpSpreadsheet\Calculation\TextData;
   4  
   5  use PhpOffice\PhpSpreadsheet\Calculation\Exception as CalcExp;
   6  use PhpOffice\PhpSpreadsheet\Calculation\Functions;
   7  
   8  class Replace
   9  {
  10      /**
  11       * REPLACE.
  12       *
  13       * @param mixed $oldText The text string value to modify
  14       * @param mixed $start Integer offset for start character of the replacement
  15       * @param mixed $chars Integer number of characters to replace from the start offset
  16       * @param mixed $newText String to replace in the defined position
  17       */
  18      public static function replace($oldText, $start, $chars, $newText): string
  19      {
  20          try {
  21              $start = Helpers::extractInt($start, 1, 0, true);
  22              $chars = Helpers::extractInt($chars, 0, 0, true);
  23              $oldText = Helpers::extractString($oldText);
  24              $newText = Helpers::extractString($newText);
  25              $left = mb_substr($oldText, 0, $start - 1, 'UTF-8');
  26  
  27              $right = mb_substr($oldText, $start + $chars - 1, null, 'UTF-8');
  28          } catch (CalcExp $e) {
  29              return $e->getMessage();
  30          }
  31  
  32          return $left . $newText . $right;
  33      }
  34  
  35      /**
  36       * SUBSTITUTE.
  37       *
  38       * @param mixed $text The text string value to modify
  39       * @param mixed $fromText The string value that we want to replace in $text
  40       * @param mixed $toText The string value that we want to replace with in $text
  41       * @param mixed $instance Integer instance Number for the occurrence of frmText to change
  42       */
  43      public static function substitute($text = '', $fromText = '', $toText = '', $instance = null): string
  44      {
  45          try {
  46              $text = Helpers::extractString($text);
  47              $fromText = Helpers::extractString($fromText);
  48              $toText = Helpers::extractString($toText);
  49              $instance = Functions::flattenSingleValue($instance);
  50              if ($instance === null) {
  51                  return str_replace($fromText, $toText, $text);
  52              }
  53              if (is_bool($instance)) {
  54                  if ($instance === false || Functions::getCompatibilityMode() !== Functions::COMPATIBILITY_OPENOFFICE) {
  55                      return Functions::Value();
  56                  }
  57                  $instance = 1;
  58              }
  59              $instance = Helpers::extractInt($instance, 1, 0, true);
  60          } catch (CalcExp $e) {
  61              return $e->getMessage();
  62          }
  63  
  64          $pos = -1;
  65          while ($instance > 0) {
  66              $pos = mb_strpos($text, $fromText, $pos + 1, 'UTF-8');
  67              if ($pos === false) {
  68                  break;
  69              }
  70              --$instance;
  71          }
  72  
  73          if ($pos !== false) {
  74              return self::REPLACE($text, ++$pos, mb_strlen($fromText, 'UTF-8'), $toText);
  75          }
  76  
  77          return $text;
  78      }
  79  }