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 310 and 400] [Versions 39 and 400]

   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      /** @var bool Whether the code is running with PHP7 or older versions */
  17      private $isRunningPhp7OrOlder;
  18  
  19      /** @var array Locale info, used for number formatting */
  20      private $localeInfo;
  21  
  22      /**
  23       *
  24       */
  25      public function __construct()
  26      {
  27          $this->hasMbstringSupport = \extension_loaded('mbstring');
  28          $this->isRunningPhp7OrOlder = \version_compare(PHP_VERSION, '8.0.0') < 0;
  29          $this->localeInfo = \localeconv();
  30      }
  31  
  32      /**
  33       * Returns the length of the given string.
  34       * It uses the multi-bytes function is available.
  35       * @see strlen
  36       * @see mb_strlen
  37       *
  38       * @param string $string
  39       * @return int
  40       */
  41      public function getStringLength($string)
  42      {
  43          return $this->hasMbstringSupport ? \mb_strlen($string) : \strlen($string);
  44      }
  45  
  46      /**
  47       * Returns the position of the first occurrence of the given character/substring within the given string.
  48       * It uses the multi-bytes function is available.
  49       * @see strpos
  50       * @see mb_strpos
  51       *
  52       * @param string $char Needle
  53       * @param string $string Haystack
  54       * @return int Char/substring's first occurrence position within the string if found (starts at 0) or -1 if not found
  55       */
  56      public function getCharFirstOccurrencePosition($char, $string)
  57      {
  58          $position = $this->hasMbstringSupport ? \mb_strpos($string, $char) : \strpos($string, $char);
  59  
  60          return ($position !== false) ? $position : -1;
  61      }
  62  
  63      /**
  64       * Returns the position of the last occurrence of the given character/substring within the given string.
  65       * It uses the multi-bytes function is available.
  66       * @see strrpos
  67       * @see mb_strrpos
  68       *
  69       * @param string $char Needle
  70       * @param string $string Haystack
  71       * @return int Char/substring's last occurrence position within the string if found (starts at 0) or -1 if not found
  72       */
  73      public function getCharLastOccurrencePosition($char, $string)
  74      {
  75          $position = $this->hasMbstringSupport ? \mb_strrpos($string, $char) : \strrpos($string, $char);
  76  
  77          return ($position !== false) ? $position : -1;
  78      }
  79  
  80      /**
  81       * Formats a numeric value (int or float) in a way that's compatible with the expected spreadsheet format.
  82       *
  83       * Formatting of float values is locale dependent in PHP < 8.
  84       * Thousands separators and decimal points vary from locale to locale (en_US: 12.34 vs pl_PL: 12,34).
  85       * However, float values must be formatted with no thousands separator and a "." as decimal point
  86       * to work properly. This method can be used to convert the value to the correct format before storing it.
  87       *
  88       * @see https://wiki.php.net/rfc/locale_independent_float_to_string for the changed behavior in PHP8.
  89       *
  90       * @param int|float $numericValue
  91       * @return string
  92       */
  93      public function formatNumericValue($numericValue)
  94      {
  95          if ($this->isRunningPhp7OrOlder && is_float($numericValue)) {
  96              return str_replace(
  97                  [$this->localeInfo['thousands_sep'], $this->localeInfo['decimal_point']],
  98                  ['', '.'],
  99                  $numericValue
 100              );
 101          }
 102  
 103          return $numericValue;
 104      }
 105  }