3 * Zend Framework (http://framework.zend.com/)
5 * @link http://github.com/zendframework/zf2 for the canonical source repository
6 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
7 * @license http://framework.zend.com/license/new-bsd New BSD License
10 namespace Zend\Stdlib\StringWrapper;
12 use Zend\Stdlib\Exception;
14 class Iconv extends AbstractStringWrapper
17 * List of supported character sets (upper case)
20 * @link http://www.gnu.org/software/libiconv/
22 protected static $encodings = [
128 // Platform specifics
150 /* Commented out because that's internal encodings not existing in real world
151 // Full Unicode, in terms of uint16_t or uint32_t (with machine dependent endianness and alignment)
155 // Locale dependent, in terms of `char' or `wchar_t' (with machine dependent endianness and alignment,
156 // and with OS and locale dependent semantics)
159 '', // The empty encoding name is equivalent to "char": it denotes the locale dependent character encoding.
162 // When configured with the option --enable-extra-encodings,
163 // it also provides support for a few extra encodings:
165 // European languages
190 'BIG5-2003', // (experimental)
195 // Platform specifics
201 * Get a list of supported character encodings
205 public static function getSupportedEncodings()
207 return static::$encodings;
213 * @throws Exception\ExtensionNotLoadedException
215 public function __construct()
217 if (! extension_loaded('iconv')) {
218 throw new Exception\ExtensionNotLoadedException(
219 'PHP extension "iconv" is required for this wrapper'
225 * Returns the length of the given string
230 public function strlen($str)
232 return iconv_strlen($str, $this->getEncoding());
236 * Returns the portion of string specified by the start and length parameters
240 * @param int|null $length
241 * @return string|false
243 public function substr($str, $offset = 0, $length = null)
245 return iconv_substr($str, $offset, $length, $this->getEncoding());
249 * Find the position of the first occurrence of a substring in a string
251 * @param string $haystack
252 * @param string $needle
256 public function strpos($haystack, $needle, $offset = 0)
258 return iconv_strpos($haystack, $needle, $offset, $this->getEncoding());
262 * Convert a string from defined encoding to the defined convert encoding
265 * @param bool $reverse
266 * @return string|false
268 public function convert($str, $reverse = false)
270 $encoding = $this->getEncoding();
271 $convertEncoding = $this->getConvertEncoding();
272 if ($convertEncoding === null) {
273 throw new Exception\LogicException(
274 'No convert encoding defined'
278 if ($encoding === $convertEncoding) {
282 $fromEncoding = $reverse ? $convertEncoding : $encoding;
283 $toEncoding = $reverse ? $encoding : $convertEncoding;
285 // automatically add "//IGNORE" to not stop converting on invalid characters
286 // invalid characters triggers a notice anyway
287 return iconv($fromEncoding, $toEncoding . '//IGNORE', $str);