3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2013 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Stdlib
;
12 use Zend\Stdlib\ErrorHandler
;
13 use Zend\Stdlib\StringWrapper\StringWrapperInterface
;
16 * Utility class for handling strings of different character encodings
17 * using available PHP extensions.
19 * Declared abstract, as we have no need for instantiation.
21 abstract class StringUtils
25 * Ordered list of registered string wrapper instances
27 * @var StringWrapperInterface[]
29 protected static $wrapperRegistry = null;
32 * A list of known single-byte character encodings (upper-case)
36 protected static $singleByteEncodings = array(
37 'ASCII', '7BIT', '8BIT',
38 'ISO-8859-1', 'ISO-8859-2', 'ISO-8859-3', 'ISO-8859-4', 'ISO-8859-5',
39 'ISO-8859-6', 'ISO-8859-7', 'ISO-8859-8', 'ISO-8859-9', 'ISO-8859-10',
40 'ISO-8859-11', 'ISO-8859-13', 'ISO-8859-14', 'ISO-8859-15', 'ISO-8859-16',
46 * Is PCRE compiled with Unicode support?
50 protected static $hasPcreUnicodeSupport = null;
53 * Get registered wrapper classes
57 public static function getRegisteredWrappers()
59 if (static::$wrapperRegistry === null) {
60 static::$wrapperRegistry = array();
62 if (extension_loaded('intl')) {
63 static::$wrapperRegistry[] = 'Zend\Stdlib\StringWrapper\Intl';
66 if (extension_loaded('mbstring')) {
67 static::$wrapperRegistry[] = 'Zend\Stdlib\StringWrapper\MbString';
70 if (extension_loaded('iconv')) {
71 static::$wrapperRegistry[] = 'Zend\Stdlib\StringWrapper\Iconv';
74 static::$wrapperRegistry[] = 'Zend\Stdlib\StringWrapper\Native';
77 return static::$wrapperRegistry;
81 * Register a string wrapper class
83 * @param string $wrapper
86 public static function registerWrapper($wrapper)
88 $wrapper = (string) $wrapper;
89 if (!in_array($wrapper, static::$wrapperRegistry, true)) {
90 static::$wrapperRegistry[] = $wrapper;
95 * Unregister a string wrapper class
97 * @param string $wrapper
100 public static function unregisterWrapper($wrapper)
102 $index = array_search((string) $wrapper, static::$wrapperRegistry, true);
103 if ($index !== false) {
104 unset(static::$wrapperRegistry[$index]);
109 * Reset all registered wrappers so the default wrappers will be used
113 public static function resetRegisteredWrappers()
115 static::$wrapperRegistry = null;
119 * Get the first string wrapper supporting the given character encoding
120 * and supports to convert into the given convert encoding.
122 * @param string $encoding Character encoding to support
123 * @param string|null $convertEncoding OPTIONAL character encoding to convert in
124 * @return StringWrapperInterface
125 * @throws Exception\RuntimeException If no wrapper supports given character encodings
127 public static function getWrapper($encoding = 'UTF-8', $convertEncoding = null)
129 foreach (static::getRegisteredWrappers() as $wrapperClass) {
130 if ($wrapperClass::isSupported($encoding, $convertEncoding)) {
131 $wrapper = new $wrapperClass($encoding, $convertEncoding);
132 $wrapper->setEncoding($encoding, $convertEncoding);
137 throw new Exception\
RuntimeException(
138 'No wrapper found supporting "' . $encoding . '"'
139 . (($convertEncoding !== null) ?
' and "' . $convertEncoding . '"' : '')
144 * Get a list of all known single-byte character encodings
148 public static function getSingleByteEncodings()
150 return static::$singleByteEncodings;
154 * Check if a given encoding is a known single-byte character encoding
156 * @param string $encoding
159 public static function isSingleByteEncoding($encoding)
161 return in_array(strtoupper($encoding), static::$singleByteEncodings);
165 * Check if a given string is valid UTF-8 encoded
170 public static function isValidUtf8($str)
172 return is_string($str) && ($str === '' ||
preg_match('/^./su', $str) == 1);
176 * Is PCRE compiled with Unicode support?
180 public static function hasPcreUnicodeSupport()
182 if (static::$hasPcreUnicodeSupport === null) {
183 ErrorHandler
::start();
184 static::$hasPcreUnicodeSupport = defined('PREG_BAD_UTF8_OFFSET_ERROR') && preg_match('/\pL/u', 'a') == 1;
185 ErrorHandler
::stop();
187 return static::$hasPcreUnicodeSupport;