You are here

class MbString in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 vendor/zendframework/zend-stdlib/src/StringWrapper/MbString.php \Zend\Stdlib\StringWrapper\MbString

Hierarchy

Expanded class hierarchy of MbString

File

vendor/zendframework/zend-stdlib/src/StringWrapper/MbString.php, line 14

Namespace

Zend\Stdlib\StringWrapper
View source
class MbString extends AbstractStringWrapper {

  /**
   * List of supported character sets (upper case)
   *
   * @var null|string[]
   * @link http://php.net/manual/mbstring.supported-encodings.php
   */
  protected static $encodings = null;

  /**
   * Get a list of supported character encodings
   *
   * @return string[]
   */
  public static function getSupportedEncodings() {
    if (static::$encodings === null) {
      static::$encodings = array_map('strtoupper', mb_list_encodings());

      // FIXME: Converting € (UTF-8) to ISO-8859-16 gives a wrong result
      $indexIso885916 = array_search('ISO-8859-16', static::$encodings, true);
      if ($indexIso885916 !== false) {
        unset(static::$encodings[$indexIso885916]);
      }
    }
    return static::$encodings;
  }

  /**
   * Constructor
   *
   * @throws Exception\ExtensionNotLoadedException
   */
  public function __construct() {
    if (!extension_loaded('mbstring')) {
      throw new Exception\ExtensionNotLoadedException('PHP extension "mbstring" is required for this wrapper');
    }
  }

  /**
   * Returns the length of the given string
   *
   * @param string $str
   * @return int|false
   */
  public function strlen($str) {
    return mb_strlen($str, $this
      ->getEncoding());
  }

  /**
   * Returns the portion of string specified by the start and length parameters
   *
   * @param string   $str
   * @param int      $offset
   * @param int|null $length
   * @return string|false
   */
  public function substr($str, $offset = 0, $length = null) {
    return mb_substr($str, $offset, $length, $this
      ->getEncoding());
  }

  /**
   * Find the position of the first occurrence of a substring in a string
   *
   * @param string $haystack
   * @param string $needle
   * @param int    $offset
   * @return int|false
   */
  public function strpos($haystack, $needle, $offset = 0) {
    return mb_strpos($haystack, $needle, $offset, $this
      ->getEncoding());
  }

  /**
   * Convert a string from defined encoding to the defined convert encoding
   *
   * @param string  $str
   * @param bool $reverse
   * @return string|false
   */
  public function convert($str, $reverse = false) {
    $encoding = $this
      ->getEncoding();
    $convertEncoding = $this
      ->getConvertEncoding();
    if ($convertEncoding === null) {
      throw new Exception\LogicException('No convert encoding defined');
    }
    if ($encoding === $convertEncoding) {
      return $str;
    }
    $fromEncoding = $reverse ? $convertEncoding : $encoding;
    $toEncoding = $reverse ? $encoding : $convertEncoding;
    return mb_convert_encoding($str, $toEncoding, $fromEncoding);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
AbstractStringWrapper::$convertEncoding protected property An optionally character encoding to convert to
AbstractStringWrapper::$encoding protected property The character encoding working on 1
AbstractStringWrapper::getConvertEncoding public function Get the defined character encoding to convert to Overrides StringWrapperInterface::getConvertEncoding
AbstractStringWrapper::getEncoding public function Get the defined character encoding to work with Overrides StringWrapperInterface::getEncoding
AbstractStringWrapper::isSupported public static function Check if the given character encoding is supported by this wrapper and the character encoding to convert to is also supported. Overrides StringWrapperInterface::isSupported 1
AbstractStringWrapper::setEncoding public function Set character encoding working with and convert to Overrides StringWrapperInterface::setEncoding 1
AbstractStringWrapper::strPad public function Pad a string to a certain length with another string Overrides StringWrapperInterface::strPad
AbstractStringWrapper::wordWrap public function Wraps a string to a given number of characters Overrides StringWrapperInterface::wordWrap
MbString::$encodings protected static property List of supported character sets (upper case)
MbString::convert public function Convert a string from defined encoding to the defined convert encoding Overrides AbstractStringWrapper::convert
MbString::getSupportedEncodings public static function Get a list of supported character encodings Overrides StringWrapperInterface::getSupportedEncodings
MbString::strlen public function Returns the length of the given string Overrides StringWrapperInterface::strlen
MbString::strpos public function Find the position of the first occurrence of a substring in a string Overrides StringWrapperInterface::strpos
MbString::substr public function Returns the portion of string specified by the start and length parameters Overrides StringWrapperInterface::substr
MbString::__construct public function Constructor