Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401]

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