Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.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      /**
  41       * Returns the position of the first occurrence of the given character/substring within the given string.
  42       * It uses the multi-bytes function is available.
  43       *
  44       * @see strpos
  45       * @see mb_strpos
  46       *
  47       * @param string $char   Needle
  48       * @param string $string Haystack
  49       *
  50       * @return int Char/substring's first occurrence position within the string if found (starts at 0) or -1 if not found
  51       */
  52      public function getCharFirstOccurrencePosition(string $char, string $string): int
  53      {
  54          $position = $this->hasMbstringSupport
  55              ? mb_strpos($string, $char)
  56              : strpos($string, $char) // @codeCoverageIgnore
  57          ;
  58  
  59          return (false !== $position) ? $position : -1;
  60      }
  61  
  62      /**
  63       * Returns the position of the last occurrence of the given character/substring within the given string.
  64       * It uses the multi-bytes function is available.
  65       *
  66       * @see strrpos
  67       * @see mb_strrpos
  68       *
  69       * @param string $char   Needle
  70       * @param string $string Haystack
  71       *
  72       * @return int Char/substring's last occurrence position within the string if found (starts at 0) or -1 if not found
  73       */
  74      public function getCharLastOccurrencePosition(string $char, string $string): int
  75      {
  76          $position = $this->hasMbstringSupport
  77              ? mb_strrpos($string, $char)
  78              : strrpos($string, $char) // @codeCoverageIgnore
  79          ;
  80  
  81          return (false !== $position) ? $position : -1;
  82      }
  83  }