MbString.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Stdlib\StringWrapper;
  10. use Zend\Stdlib\Exception;
  11. class MbString extends AbstractStringWrapper
  12. {
  13. /**
  14. * List of supported character sets (upper case)
  15. *
  16. * @var null|string[]
  17. * @link http://php.net/manual/mbstring.supported-encodings.php
  18. */
  19. protected static $encodings = null;
  20. /**
  21. * Get a list of supported character encodings
  22. *
  23. * @return string[]
  24. */
  25. public static function getSupportedEncodings()
  26. {
  27. if (static::$encodings === null) {
  28. static::$encodings = array_map('strtoupper', mb_list_encodings());
  29. // FIXME: Converting € (UTF-8) to ISO-8859-16 gives a wrong result
  30. $indexIso885916 = array_search('ISO-8859-16', static::$encodings, true);
  31. if ($indexIso885916 !== false) {
  32. unset(static::$encodings[$indexIso885916]);
  33. }
  34. }
  35. return static::$encodings;
  36. }
  37. /**
  38. * Constructor
  39. *
  40. * @throws Exception\ExtensionNotLoadedException
  41. */
  42. public function __construct()
  43. {
  44. if (!extension_loaded('mbstring')) {
  45. throw new Exception\ExtensionNotLoadedException(
  46. 'PHP extension "mbstring" is required for this wrapper'
  47. );
  48. }
  49. }
  50. /**
  51. * Returns the length of the given string
  52. *
  53. * @param string $str
  54. * @return int|false
  55. */
  56. public function strlen($str)
  57. {
  58. return mb_strlen($str, $this->getEncoding());
  59. }
  60. /**
  61. * Returns the portion of string specified by the start and length parameters
  62. *
  63. * @param string $str
  64. * @param int $offset
  65. * @param int|null $length
  66. * @return string|false
  67. */
  68. public function substr($str, $offset = 0, $length = null)
  69. {
  70. return mb_substr($str, $offset, $length, $this->getEncoding());
  71. }
  72. /**
  73. * Find the position of the first occurrence of a substring in a string
  74. *
  75. * @param string $haystack
  76. * @param string $needle
  77. * @param int $offset
  78. * @return int|false
  79. */
  80. public function strpos($haystack, $needle, $offset = 0)
  81. {
  82. return mb_strpos($haystack, $needle, $offset, $this->getEncoding());
  83. }
  84. /**
  85. * Convert a string from defined encoding to the defined convert encoding
  86. *
  87. * @param string $str
  88. * @param bool $reverse
  89. * @return string|false
  90. */
  91. public function convert($str, $reverse = false)
  92. {
  93. $encoding = $this->getEncoding();
  94. $convertEncoding = $this->getConvertEncoding();
  95. if ($convertEncoding === null) {
  96. throw new Exception\LogicException(
  97. 'No convert encoding defined'
  98. );
  99. }
  100. if ($encoding === $convertEncoding) {
  101. return $str;
  102. }
  103. $fromEncoding = $reverse ? $convertEncoding : $encoding;
  104. $toEncoding = $reverse ? $encoding : $convertEncoding;
  105. return mb_convert_encoding($str, $toEncoding, $fromEncoding);
  106. }
  107. }