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