Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 402 and 403]

   1  <?php
   2  
   3  declare(strict_types=1);
   4  
   5  namespace OpenSpout\Common\Helper;
   6  
   7  /**
   8   * @internal
   9   */
  10  final class StringHelper
  11  {
  12      /** @var bool Whether the mbstring extension is loaded */
  13      private bool $hasMbstringSupport;
  14  
  15      public function __construct(bool $hasMbstringSupport)
  16      {
  17          $this->hasMbstringSupport = $hasMbstringSupport;
  18      }
  19  
  20      public static function factory(): self
  21      {
  22          return new self(\function_exists('mb_strlen'));
  23      }
  24  
  25      /**
  26       * Returns the length of the given string.
  27       * It uses the multi-bytes function is available.
  28       *
  29       * @see strlen
  30       * @see mb_strlen
  31       */
  32      public function getStringLength(string $string): int
  33      {
  34          return $this->hasMbstringSupport
  35              ? mb_strlen($string)
  36              : \strlen($string); // @codeCoverageIgnore
  37      }
  38  
  39      /**
  40       * Returns the position of the first occurrence of the given character/substring within the given string.
  41       * It uses the multi-bytes function is available.
  42       *
  43       * @see strpos
  44       * @see mb_strpos
  45       *
  46       * @param string $char   Needle
  47       * @param string $string Haystack
  48       *
  49       * @return int Char/substring's first occurrence position within the string if found (starts at 0) or -1 if not found
  50       */
  51      public function getCharFirstOccurrencePosition(string $char, string $string): int
  52      {
  53          $position = $this->hasMbstringSupport
  54              ? mb_strpos($string, $char)
  55              : strpos($string, $char); // @codeCoverageIgnore
  56  
  57          return (false !== $position) ? $position : -1;
  58      }
  59  
  60      /**
  61       * Returns the position of the last occurrence of the given character/substring within the given string.
  62       * It uses the multi-bytes function is available.
  63       *
  64       * @see strrpos
  65       * @see mb_strrpos
  66       *
  67       * @param string $char   Needle
  68       * @param string $string Haystack
  69       *
  70       * @return int Char/substring's last occurrence position within the string if found (starts at 0) or -1 if not found
  71       */
  72      public function getCharLastOccurrencePosition(string $char, string $string): int
  73      {
  74          $position = $this->hasMbstringSupport
  75              ? mb_strrpos($string, $char)
  76              : strrpos($string, $char); // @codeCoverageIgnore
  77  
  78          return (false !== $position) ? $position : -1;
  79      }
  80  }